Reputation: 309
Please, help me. I have a variable lang and i know that after this variable must go characters -(.+);
For example, i would have written it in C # so - regexString = lang + "-(.+);";
But in javascript this code not correct: str1 = str1.replace(lang+"-(.+);", replacement);
because there must use /.../, but i don't know how to write correct
Upvotes: 0
Views: 119
Reputation: 351698
Try this:
var re = new RegExp(lang+"-(.+);");
str1 = str1.replace(re, replacement);
Upvotes: 0
Reputation: 53881
There is a dynamic way to create regexps in javascript:
new RegExp(lang+'\-(.+)')
(I also escaped the -
.)
So to do a replace:
str.replace(new Regexp(lang+'\-(.+)'), replacement)
If you want to replace more than 1:
str.replace(new Regexp(lang+'\-(.+)', 'g'), replacement)
The 'g'
flag is for 'global'.
Upvotes: 1