javascript \d regular expression unexpected behavior - javascript

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");

Related

why does this js RegExp test return true? [duplicate]

The regex allows chars that are: alphanumeric, space, '-', '_', '&', '()' and '/'
this is the expression
[\s\/\)\(\w&-]
I have tested this in various online testers and know it works, I just can't get it to work correctly in code. I get sysntax errors with anything I try.. any suggestions?
var programProductRegex = new RegExp([\s\/\)\(\w&-]);
You can use the regular expression syntax:
var programProductRegex = /[\s\/\)\(\w&-]/;
You use forward slashes to delimit the regex pattern.
If you use the RegExp object constructor you need to pass in a string. Because backslashes are special escape characters inside JavaScript strings and they're also escape characters in regular expressions, you need to use two backslashes to do a regex escape inside a string. The equivalent code using a string would then be:
var programProductRegex = new RegExp("[\\s\\/\\)\\(\\w&-]");
All the backslashes that were in the original regular expression need to be escaped in the string to be correctly interpreted as backslashes.
Of course the first option is better. The constructor is helpful when you obtain a string from somewhere and want to make a regular expression out of it.
var programProductRegex = new RegExp(userInput);
If you are using a String and want to escape characters like (, you need to write \\( (meaning writing backslash, then the opening parenthesis => escaping it).
If you are using the RegExp object, you only need one backslash for each character (like \()
Enclose your regex with delimiters:
var programProductRegex = /[\s\/)(\w&-]/;

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

Regex - Invalid Quantifier

I am using this regex in JavaScript which is to evaluate if a given string matches some german phone number patterns.
var reg = new RegExp("(?:\+\d+)?\s*(?:\(\d+\)\s*(?:[/–-]\s*)?)?\d+(?:\s*(?:[\s/–-]\s*)?\d+)*");
When using it I get this error:
SyntaxError: invalid quantifier
...eg = new RegExp("(?:\+\d+)\s*(?:\(\d+\)\s*(?:[/–-]\s*))\d+(?:\s*(?:[\s/–-]\s*)\d...
I'm trying hard to learn reading regular expressions, but can not understand them full yet. I did not write this expression by myself and I am struggling to understand it.
Why I am getting this error?
Because you're using a string litteral, you need to escape each backslash:
(?:\\+\\d+)?\\s*(?:\\(\\d+\\)\\s*(?:[/–-]\\s*)?)?\\d+(?:\\s*(?:[\\s/–-]\\s*)?\\d+)*
The other solution would be to use the regex litteral:
var reg = /(?:\+\d+)?\s*(?:\(\d+\)\s*(?:[\/–-]\s*)?)?\d+(?:\s*(?:[\s\/–-]\s*)?\d+)*/;
new RegExp requires a string, and since backslashes already have meaning inside strings, they need to be escaped again.
In your case, though, you're using a static pattern, so you'd be better off with a literal:
var reg = /(?:\+\d+)?\s*(?:\(\d+\)\s*(?:[\/–-]\s*)?)?\d+(?:\s*(?:[\s\/–-]\s*)?\d+)*/;
Just be aware that you need to escape / here ;)
As an additional tip, you can simplify the need to escape things to some extent by doing stuff like [+] for a literal +. I think it looks nicer then \+, but that's just my opinion.

Why cant I get my Regular expression working?

What am I doing wrong as both strings below are returning false when tested below?
var pattern = "^[\s\da-zA-ZåäöÅÄÖ_]+$"
var reg = new RegExp(pattern);
console.log(reg.test("This should be invalid as it is full with invalid chars. #!¤%&/()=?"));
console.log(reg.test("This is an valid string, despite that Swedish chars such as ÅÄÖ are used"));
You need to double-up on the backslashes in the pattern.
var pattern = "^[\\s\\da-zA-ZåäöÅÄÖ_]+$"
The problem is that when you build regular expression objects that way, there are two passes made over the string: one to interpret it as a string, and then a second to interpret it as a regular expression. Both of those micro-syntaxes use \ to mean something, so by doubling them you get a single backslash out of the string constant parse.
If your pattern is really a constant, and not something that you construct dynamically from separate parts, then you can just use the native syntax for regular expressions:
var pattern = /^[\s\da-zA-ZåäöÅÄÖ_]+$/;
Only one backslash is necessary because the pattern is only parsed once, as a regular expression.

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;

Categories

Resources