Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to add a (variable) tag to values with regex, the pattern works fine with PHP but I have troubles implementing it into JavaScript.

The pattern is (value is the variable):

/(?!(?:[^<]+>|[^>]+<\/a>))\b(value)\b/is

I escaped the backslashes:

var str = $("#div").html();
var regex = "/(?!(?:[^<]+>|[^>]+<\\/a>))\\b(" + value + ")\\b/is";
$("#div").html(str.replace(regex, "<a href='#" + value +">" + value + "</a>"));

But this seem not to be right, I logged the pattern and its exactly what it should be. Any ideas?

share|improve this question
 
Is value a variable? –  Dogbert Jul 26 '13 at 15:57
add comment

3 Answers

up vote 0 down vote accepted

There is no s (dotall) flag/modifier in JavaScript. It usually forces the dot (.) to match newlines, but this doesn't matter to your expression, since you don't use ..

Besides that, to create the regex as string, you have to use JavaScript's RegExp object. Demo code goes below:

var str = $("#div").html();
var regexExpression ="(?!(?:[^<]+>|[^>]+<\\/a>))\\b(" + value + ")\\b";
var regex = new RegExp(regexExpression, "i");
$("#div").html(str.replace(regex, "<a href='#" + value +"'>" + value + "</a>"));

Notice I added a missing ' at the end of "<a href='#" + value +"'>".

See demo fiddle here.



You may want to match/replace more than one time. In that case, add the g (global match) flag:

var regex = new RegExp(regexExpression, "ig");

See fiddle demonstrating what I mean here.

share|improve this answer
 
Thanks, that worked –  Borstenhorst Jul 26 '13 at 16:32
add comment

If you are trying to use a variable value in the expression, you must use the RegExp "constructor".

var regex="(?!(?:[^<]+>|[^>]+<\/a>))\b(" + value + ")\b";
new RegExp(regex, "is")
share|improve this answer
add comment

You don't need the " to define a regular expression so just:

var regex = /(?!(?:[^<]+>|[^>]+<\/a>))\b(value)\b/is; // this is valid syntax

If value is a variable and you want a dynamic regular expression then you can't use this notation; use the alternative notation.

String.replace also accepts strings as input, so you can do "fox".replace("fox", "bear");

Alternative:

var regex = new RegExp("/(?!(?:[^<]+>|[^>]+<\/a>))\b(value)\b/", "is");
var regex = new RegExp("/(?!(?:[^<]+>|[^>]+<\/a>))\b(" + value + ")\b/", "is");
var regex = new RegExp("/(?!(?:[^<]+>|[^>]+<\/a>))\b(.*?)\b/", "is");

Keep in mind that if value contains regular expressions characters like (, [ and ? you will need to escape them.

share|improve this answer
 
The first option would not work unless looking for the string "value" –  happytime harry Jul 26 '13 at 16:00
 
@happytimeharry There seems a conflict between the two regexps he posted. –  Doge Jul 26 '13 at 16:03
 
@Fritz van Campen it seems that the intent of the pattern, given the example from his javascript, was to use a variable –  happytime harry Jul 26 '13 at 16:04
 
yes sorry, value is a variable –  Borstenhorst Jul 26 '13 at 16:07
 
same problem here, there seems to be something wrong with the "is" flag: "Uncaught SyntaxError: Invalid flags supplied to RegExp constructor 'is' " –  Borstenhorst Jul 26 '13 at 16:10
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.