JavaScript Regex to Java Regex - Replace and Lambdas
By : Jerry Marmol
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further What is the best way to approach translating this code into Java? (the section called Convert Alpha-numeric Phone Number to All Numeric) http://lawrence.ecorp.net/inet/samples/regexp-format.php , Wow, the original code is a bit over-verbose. I'd do: code :
return phoneStr.replace(/[a-zA-Z]/g, function(m) {
var ix= m[0].toLowerCase().charCodeAt(0)-0x61; // ASCII 'a'
return '22233344455566677778889999'.charAt(ix);
});
StringBuffer b= new StringBuffer();
Matcher m= Pattern.compile("[A-Za-z]").matcher(phoneStr);
while (m.find()) {
int ix= (int) (m.group(0).toLowerCase().charAt(0)-'a');
m.appendReplacement(b, "22233344455566677778889999".substring(ix, ix+1));
}
m.appendTail(b);
return b.toString();
char[] c= phoneStr.toLowerCase().toCharArray();
for (int i= 0; i<c.length; i++)
if (c[i]>='a' && c[i]<='z')
c[i]= "22233344455566677778889999".charAt((int) (c[i]-'a'));
return new String(c);
|
javascript regex replace, how to provide regex condition (and flags) as variable instead of inline?
By : Chirag
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Using the JS replace function with regex, and will have dozens of replace statements. , I would use a lookup object to map the originals to URLs: code :
// Original string
var o = "MySql is a DBMS, whereas javascript is a client side scripting language";
//Patterns
var patterns = {
"javascript": "http://js.com/",
"mysql": "http://www.mysql.com/"
};
//Constructing regex
RegExp.escape= function(s) {
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
};
var keys = [];
for (i in patterns) {
if (patterns.hasOwnProperty(i)) {
keys.push(RegExp.escape(i));
}
}
var pattern = new RegExp("\\b(" + keys.join("|") + ")\\b", "gi");
//Replace
var n = o.replace(pattern, function(m, g1) {
return "<a href='" + patterns[g1.toLowerCase()] + "'>" + g1 + "< /a>";
});
console.log(n);
// Original string
var o = '<p>Test 1 (JavaScript - <strong>1st keyword instance to be replaced</strong>): <br><a href="http://js1.net">Link to JavaScript site (existing URL)</a> is a scripting language commonly implemented as part of a web browser in order to create enhanced user interfaces and dynamic websites. JavaScript is very flexible.</p><p>more text here... and another mention of JavaScript. also javascript and JAVAScrIPT <br><br></p><p>Test 2 (MySQL - <strong>1st keyword instance to be replaced</strong>): <br><a href="http://www.mysql.com">MySQL</a> (existing URL) is the most popular open-source database system.</p> <p><a href="http://www.themysqllink.com">link to a MySQL site</a> (existing URL).</p><p> More stuff about Mysql, also mysql and mySQL</p>';
//Patterns
var patterns = {
"javascript": "http://js.com/",
"mysql": "http://www.mysql.com/",
"mention": "http://www.x.com/"
};
//Number of replacements
var num = 1;
//Constructing regex
RegExp.escape = function(s) {
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};
var keys = [];
for (key in patterns) {
if (patterns.hasOwnProperty(key)) {
keys.push(RegExp.escape(key));
}
}
var regexen = [];
for (var i = 0; i < keys.length; i++) {
regexen[i] = new RegExp("\\b(" + keys[i] + ")\\b(?![^<]*?<\/a>)", "i");
}
//Replace
for (var i = 0; i < regexen.length; i++) {
var count = 0;
var pattern = regexen[i];
while (count < num) {
o = o.replace(pattern, function(m, g1) {
return "<a href='" + patterns[g1.toLowerCase()] + "'>" + g1 + "</a>";
});
count++;
}
}
document.write(o);
|
android java, regex conditional replace sub content of the match (like javascript's str.replace(pattern, function(){})
By : Mark McQuillen
Date : March 29 2020, 07:55 AM
will be helpful for those in need Looks like using matcher may help to get the same. Is there better approach? code :
String passedInToken = "this is the token";
String srcPath = "/pathabc";
StringBuffer sb = new StringBuffer();
String srcPatternString = "(src=\".*?\")";
Pattern srcPattern = Pattern.compile(srcPatternString);
String patternString = "(?:<img\\s)([^<]*)(?:token=\""+passedInToken+"\"\\s*)([^>]*)>";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(htmlStr);
while(matcher.find()) {
String srcPart = matcher.group(1) + " " + matcher.group(2);
StringBuffer srcSb = new StringBuffer();
Matcher strMatcher = srcPattern.matcher(srcPart);
while (strMatcher.find()) {
strMatcher.appendReplacement(srcSb, strMatcher.group(1)+ srcPath);
}
strMatcher.appendTail(srcSb);
String srcStr = "<img \"" + srcSb.toString() + ">";
matcher.appendReplacement(sb, srcStr);
}
matcher.appendTail(sb);
String newHtmlStr = sb.toString();
|
Regex: Replace Parentheses in JavaScript code with Regex
By : user2258957
Date : March 29 2020, 07:55 AM
I hope this helps you . You don't have the right syntax for regex literals. They're written as code :
/pattern/flags
var foo = li.replace(/[\s'()]+/g, "");
|
RegEx replace in Javascript doesn't replace
By : Tim
Date : March 29 2020, 07:55 AM
hope this fix your issue You have to pass a regular expression, not a string: code :
var result = "/wiki/home/de/wikihome.md".replace(/(\/wiki\/home)\/..\/(.*\.md)/, "$1/en/$2");
document.body.innerHTML = result;
|