I want to build a RegEx in JavaScript that matches a word but not part of it. I think that something like \bword\b works well for this. My problem is that the word is not known in advance so I would like to assemble the regular expression using a variable holding the word to be matched something along the lines of:
r = "\b(" + word + ")\b";
reg = new RegExp(r, "g");
lexicon.replace(reg, "<span>$1</span>"
which I noticed, does not work. My idea is to replace specific words in a paragraph with a span tag. Can someone help me?
PS: I am using jQuery.
\ is an escape character in regular expressions and in strings.
Since you are assembling the regular expression from strings, you have to escape the \s in them.
r = "\\b(" + word + ")\\b";
should do the trick, although I haven't tested it.
You probably shouldn't use a global for r though (and probably not for reg either).
You're not escaping the backslash. So you should have:
r = "\\b(" + word + ")\\b"; //Note the double backslash
reg = new RegExp(r, "g");
Also, you should escape special characters in 'word', because you don't know if it can have regex special characters.
Hope this helps. Cheers
And don't write expressions in the regexp variable, because it does not work!
Example(not working):
var r = "^\\w{0,"+ maxLength-1 +"}$"; // causes error
var reg = new RegExp(r, "g");
Example, which returns the string as expected:
var m = maxLength - 1;
var r = "^\\w{0,"+ m +"}$";
var reg = new RegExp(r, "g");
Use this
r = "(\\\b" +word+ "\\\b)"
Related
I am trying to find a match in a string with JavaScript. I want to work with the RegEx function.
My example (what I have tried):
var str = "hello.you";
var patt1 = '\\b' + str + '\\b';
var result = str.match(patt1);
But this does not give me the result which I except. I just want to print "you".
Thanks all in advance.
So you jumped right into a pretty advanced regex topic. You sort of want to do a lookahead (the word AFTER a given boundary character). The following will get you there:
let str = "hello.you",
myRegex = /(?<=\.)\w+/;
let theWord = str.match(myRegex);
console.log(theWord[0]);
... And what that does, is uses (?<=.) to indicate "something that comes after a period", followed by \w+ to indicate a word.
I'd recommend using a regex tester, and build from that. I use https://www.regextester.com/
I read on RegExp in javascript
And I see two ways to create new RegExp:/ab+c/i;
new RegExp('ab+c', 'i');
new RegExp(/ab+c/, 'i');
But I want to create new RegExp like this:
var re = new RegExp(`\d+${variable}`, 'g');
I try but it's not working. How can I do that?
Escape your RegExp character class in the template literal with a \\, e.g. to escape:
\d, write \\d
\w, write \\w
\s, write \\s
...and so on.
let variable = 1;
const re = new RegExp(`\\d+${variable}`, 'g');
console.log(re);
More on RegeExp
You can write the whole expression as a string (to which you can concatenate the value of the variable), then use eval() to change it to a working javascript expression:
var x = "A";
var re = eval("new RegExp('\d+$" + x + "', 'g')");
I am using regex to replace ( in other regexes (or regexs?) with (?: to turn them into non-matching groups. My expression assumes that no (?X structures are used and looks like this:
(
[^\\] - Not backslash character
|^ - Or string beginning
)
(?:
[\(] - a bracket
)
Unfortunatelly this doesn't work in case that there are two matches next to each other, like in this case: how((\s+can|\s+do)(\s+i)?)?
With lookbehinds, the solution is easy:
/(?<=[^\\]|^)[\(]/g
But javascript doesn't support lookbehinds, so what can I do? My searches didn't bring any easy universal lookbehind alternative.
Use lookbehind through reversal:
function revStr(str) {
return str.split('').reverse().join('');
}
var rx = /[(](?=[^\\]|$)/g;
var subst = ":?(";
var data = "how((\\s+can|\\s+do)(\\s+i)?)?";
var res = revStr(revStr(data).replace(rx, subst));
document.getElementById("res").value = res;
<input id="res" />
Note that the regex pattern is also reversed so that we could use a look-ahead instead of a look-behind, and the substitution string is reversed, too. It becomes too tricky with longer regexps, but in this case, it is still not that unreadable.
One option is to do a two-pass replacement, with a token (I like unicode for this, as it's unlikely to appear elsewhere):
var s = 'how((\\s+can|\\s+do)(\\s+i)?)?';
var token = "\u1234";
// Look for the character preceding the ( you want
// to replace. We'll add the token after it.
var patt1 = /([^\\])(?=\()/g;
// The second pattern looks for the token and the (.
// We'll replace both with the desired string.
var patt2 = new RegExp(token + '\\(', 'g');
s = s.replace(patt1, "$1" + token).replace(patt2, "(?:");
console.log(s);
https://jsfiddle.net/48e75wqz/1/
(EDITED)
string example:
how((\s+can|\s+do)(\s+i)?)?
one line solution:
o='how((\\s+can|\\s+do)(\\s+i)?)?';
alert(o.replace(/\\\(/g,9e9).replace(/\(/g,'(?:').replace(/90{9}/g,'\\('))
result:
how(?:(?:\s+can|\s+do)(?:\s+i)?)?
and of course it works with strings like how((\s+\(can\)|\s+do)(\s+i)?)?
I've got a regular expression like this one:
var reg = /http:\/\/s(\d+)\.de\.example\.com\/(.*?)\.php(.*)/i;
I want to have a variable, exactly this: ccode[i][2] in place of .de.
How do I do that? Thank for your help.
You need to use a RegExp constructor notation if you want to use variables in your regex pattern, and inside it, you need to double escape special regex metacharacters. I.e.
var reg = new RegExp("http://s(\\d+)\\." + ccode[i][2] + "\\.example\\.com/(.*?)\\.php(.*)", "i");
From MDN:
Use the constructor function when you know the regular expression pattern will be changing, or you don't know the pattern and are getting it from another source, such as user input.
Sample code:
var ccode = "de";
var reg = new RegExp("http://s(\\d+)\\." + ccode + "\\.example\\.com/(.*?)\\.php(.*)", "i");
alert(reg.test("http://s123.de.example.com/something.php?some=params"));
Try this
var reg = '/http:\/\/s(\d+)\\'+ccode[i][2]+'\.example\.com\/(.*?)\.php(.*)/i';
Im using this snippet to replace several characters in a string.
var badwords = eval("/foo|bar|baz/ig");
var text="foo the bar!";
document.write(text.replace(badwords, "***"));
But one of the characters I want to replace is '/'. I assume it's not working because it's a reserved character in regular expressions, but how can I get it done then?
Thanks!
You simply escape the "reserved" char in your RegExp:
var re = /abc\/def/;
You are probably having trouble with that because you are, for some reason, using a string as your RegExp and then evaling it...so odd.
var badwords = /foo|bar|baz/ig;
is all you need.
If you INISIST on using a string, then you have to escape your escape:
var badwords = eval( "/foo|ba\\/r|baz/ig" );
This gets a backslash through the JS interpreter to make it to the RegExp engine.
first of DON'T USE EVAL it's the most evil function ever and fully unnecessary here
var badwords = /foo|bar|baz/ig;
works just as well (or use the new RegExp("foo|bar|baz","ig"); constructor)
and when you want to have a / in the regex and a \ before the character you want to escape
var badwords = /\/foo|bar|baz/ig;
//or
var badwords = new RegExp("\\/foo|bar|baz","ig");//double escape to escape the backslash in the string like one has to do in java