Using a string variable as regular expression - javascript

In JavaScript, we append /g to an unquoted string to denote a regular expression.
What if I have a string in a variable and want to use it as a regular expression?
Is this possible? If so, can anyone show me some example code?
Thanks.

Use this:
new RegExp("your regex here", "modifiers");
And notice that /g is not the delimiter for a regex, it is global modifier. A regex looks like this: /your regex here/modifiers. modifiers can be a combination of g, i and m. They are all explained here: http://www.regular-expressions.info/javascript.html

/g is a flag denoting global ( match all instances of the regex ), it doesn't denote a regular expression but is simply a flag.
If you want a dynamic regex use new RegExp. Usage here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp

Thanks, Gabi, you helpmed a lot. However, I had to put the regexp part (the variable part) without quotes to make it work:
new RegExp(yourRegExp, "modifiers").
I hope this helps someone.

Related

What is the best way to write this RegEx expression for JavaScript

I have a string containing from input an script tag, and I want to transform it to something that can be latter written inside another script tag (follow me?), therefore I need to change '</script>' and also '</SCRIPT>' into '</scr'+'ipt>'
What is the best way to do this? I already have this, but it feels improbable that this is the best solution...
tag = tag.replace(new RegExp("</[sS][cC][rR][iI][pP][tT]>", "g"),"</scr'+'ipt>");
You could use the case insensitive modifier:
tag = tag.replace(new RegExp("</script>", "gi"),"</scr'+'ipt>");
// here _^
In JavaScript, you can use /i regex modifier that makes the whole pattern case-insensitive.
Besides, there are two ways to declare a RegExp: with a literal or constructor notation. Use the former when you have a known pattern, and use the latter when the pattern is built dynamically.
So, the best way to do what you need is using a literal notation with /i modifier:
tag = tag.replace(/<\/script>/gi, "</scr'+'ipt>");
^ ^ ^
Note that in literal notation, forward slashes must be escaped, but you do not have to double escape shorthand classes and special regex characters (e.g. \\s, \\w, etc.).
Try to add the case-insensitive flag i
tag = tag.replace(new RegExp("</script>", "g", "i"),"</scr'+'ipt>");

javascript check if string contains any symbols

I have the following set of symbols:
var a = '|\/~^:,;?!&%$#*+';
How can I check is the following string contains any of those symbols?
var b = 'avguybdf';
As suggested, regular expressions will work.
b.match(/[|\\/~^:,;?!&%$#*+]/);
EDIT: I originally used the method here https://stackoverflow.com/a/6969486/2044733 to escape the string but because of the grouping, only the backslash character needs to be escaped.
The "/" at the beginning and end of the string are the delimiters for regular expressions in javascript, and "[]" are used to group the characters. In case you're wondering how this works.
Use RegEx
Check the how to use regex # Javascript RegEx
Try one of the following examples that use regular expressions:
http://www.webdeveloper.com/forum/showthread.php?264705-Best-way-to-check-for-multiple-characters-in-a-string
http://tjvantoll.com/2013/03/14/better-ways-of-comparing-a-javascript-string-to-multiple-values/
Use RegEx. You can use test() or exec(). Read more here: http://www.w3schools.com/jsref/jsref_obj_regexp.asp

regexp match _ or no character

I have a small problem with JavaScript regexp.
I want to match a part of the html class, something like:
req_password_sameAs-myid_min-6
A want to match sameAs-myid only, but with idea that there is a possibility to not have other characters after this string for example:
req_password_sameAs-myid
is also an option.
I use this expression
detectCase[i].match(/sameAs-.*?(_|)/g)
but don't know how to tell regexp _ or no characters as you can see.
Thank you in advance
Regex quantifiers. _? is the same as _{0,1}, means 'an underscore or nothing'.
You should be more specific than .*?. That's your problem, not the syntactically correct (but ugly) way of making the underscore optional.
Try
/sameAs-[a-zA-Z]*/g

Brackets in Regular Expression

I'd like to compare 2 strings with each other, but I got a little problem with the Brackets.
The String I want to seek looks like this:
CAPPL:LOCAL.L_hk[1].vorlauftemp_soll
Quoting those to bracket is seemingly useless.
I tried it with this code
var regex = new RegExp("CAPPL:LOCAL.L_hk\[1\].vorlauftemp_soll","gi");
var value = "CAPPL:LOCAL.L_hk[1].vorlauftemp_soll";
regex.test(value);
Somebody who can help me??
It is useless because you're using string. You need to escape the backslashes as well:
var regex = new RegExp("CAPPL:LOCAL.L_hk\\[1\\].vorlauftemp_soll","gi");
Or use a regex literal:
var regex = /CAPPL:LOCAL.L_hk\[1\].vorlauftemp_soll/gi
Unknown escape characters are ignored in JavaScript, so "\[" results in the same string as "[".
In value, you have (1) instead of [1]. So if you expect the regular expression to match and it doesn't, it because of that.
Another problem is that you're using "" in your expression. In order to write regular expression in JavaScript, use /.../g instead of "...".
You may also want to escape the dot in your expression. . means "any character that is not a line break". You, on the other hand, wants the dot to be matched literally: \..
You are generating a regular expression (in which [ is a special character that can be escaped with \) using a string (in which \ is a special character).
var regex = /CAPPL:LOCAL.L_hk\[1\].vorlauftemp_soll/gi;

javascript \d regular expression unexpected behavior

I am trying use javascript regular expressions to do some matching and I found a really unusual behavior that I was hoping someone could explain.
The string I was trying to match was: " 0 (IR) " and the code block was
finalRegEx = new RegExp("[0-9]");
match = finalRegEx.exec(str);
except that when I put "\d" instead of "[0-9]" it didn't find a match. I'm really confused by this.
If you use RegExp with "\d" to build the regular expression, the "\d" will result in just "d". Either use two back slashes to escape the slash like "\\d" or simply use the regular expression literals /…/ instead:
match = /\d/.exec(str)
You need to escape it because you're using the constructor, otherwise it matches d literally:
new RegExp('\\d').test('1')
new RegExp should only be used for dynamic matching. Otherwise use a literal:
var foo = /\d/;
foo.test(1)
You probably need to escape the backslash: finalRegEx = new RegExp("\\d");

Categories

Resources