Javascript Regex Object Doesn't Recognize {n,m} - javascript

I'm trying to write a regular expression in JS to recognize any digit up to seven times, followed by a "-" followed by 2 digits followed by "-" followed by a single digit. This is the simple regex I have:
/\d{1,7}-\d{2}-\d/g
This should match strings like:
123-12-7
1-12-7
1234567-12-7
but not 12345678-12-1
However, the above is returning true. The regex returns true when there is any number of digit in the first group.
Does the JavaScript Regex object not support {n,m}?
Here is an example of what I am talking about.
var pattern = new RegExp(/\d{1,7}-\d{2}-\d/);
alert(pattern.test("12345678-13-1"));
http://jsfiddle.net/XTRAc/1/ live example

It matches 2345678-13-1. You need to anchor it to the beginning and end of your string:
/^\d{1,7}-\d{2}-\d$/
Note though, that (as Rocket Hazmat pointed out) you do not need to use the RegExp constructor if you use a regex literal (something without string quotes).
JSFiddle

It does support the {min,max}-syntax, but .match and .test() try to find matching substrings. You will have to include start and end anchors. Also notice that you should either use the RegExp constructor to build a regex from a string or a regex literal, but not both (see MDN: creating regexes).
/^\d{1,7}-\d{2}-\d$/
new RegExp("^\\d{1,7}-\\d{2}-\\d$") // the worse choice

You are constructing your regex incorrectly. Try this (note the anchors, which ensure the string consists of nothing but your pattern):
var pattern= /^\d{1,7}-\d{2}-\d$/;
Otherwise subsets of the existing string will match your regex.

If you need to validate entire input string, use regex pattern
/^\d{1,7}-\d{2}-\d$/
If you need to validate entire line of input string, use regex pattern
/^\d{1,7}-\d{2}-\d$/mg
If you need to find matches within input string, use regex pattern
/(?:\D|^)(\d{1,7}-\d{2}-\d)(?!\d)/g
...and use $1 as a result.

It does support the {n,m} part, the problem here is that your example matches 2345678, so you would need a way of matching the character before the first set of digits

Related

How to replace a substring with open parentheses (

I am a Regex newbie and trying to implement Regex to replace a matching pattern in a string only when it has a ( - open parentheses using Javascript. for example if I have a string
IN(INTERM_LEVEL_IN + (int)X_ID)
I would only like to highlight the first IN( in the string. Not the INTERM_LEVEL_IN (2 ins here) and the int.
What is the Regex to accomplish this?
To match the opening bracket you just need to escape it: IN\(.
For instance, running this in Firebug console:
enter code here"IN(INTERM_LEVEL_IN + (int)X_ID)".replace(/(IN()/, 'test');`
Will result in:
>>> "IN(INTERM_LEVEL_IN + (int)X_ID)".replace(/(IN\()/, 'test');
"testINTERM_LEVEL_IN + (int)X_ID)"
Parenthesis in regular expressions have a special meaning (sub-capture groups), so when you want them to be interpreted literally you have to escape them by with a \ before them. The regular expression IN\( would match the string IN(.
The following should only match IN( at the beginning of a line:
/^IN\(/
The following would match IN( that is not preceded by any alphanumeric character or underscore:
/[a-zA-Z0-9_]IN\(/
And finally, the following would match any instance of IN( no matter what precedes it:
/IN\(/
So, take your pick. If you're interested in learning more about regex, here's a good tutorial: http://www.regular-expressions.info/tutorial.html
You can use just regular old Javascript for regex, a simple IN\( would work for the example you gave (see here), but I suspect your situation is more complicated than that. In which case, you need to define exactly what you are trying to match and what you don't want to match.

Regex to count the number of capturing groups in a regex

I need a regex that examines arbitrary regex (as a string), returning the number of capturing groups. So far I have...
arbitrary_regex.toString().match(/\((|[^?].*?)\)/g).length
Which works for some cases, where the assumption that any group that starts with a question mark, is non-capturing. It also counts empty groups.
It does not work for brackets included in character classes, or escaped brackets, and possibly some other scenarios.
Modify your regex so that it will match an empty string, then match an empty string and see how many groups it returns:
var num_groups = (new RegExp(regex.toString() + '|')).exec('').length - 1;
Example: http://jsfiddle.net/EEn6G/
The accepted answer is what you should use in any production system. However, if you wanted to solve it using a regex for fun, you can do that as shown below. It assumes the regex you want the number of groups in is correct.
Note that the number of groups is just the number of non-literal (s in the regex. The strategy we're going to take is instead of matching all the correct (, we're going to split on all the incorrect stuff in between them.
re.toString().split(/(\(\?|\\\[|\[(?:\\\]|.)*?\]|\\\(|[^(])+/g).length - 1
You can see how it works on www.debuggex.com.

Regex format from PHP to Javascript

Can you please help me. How can I add this regex (?<=^|\s):d(?=$|\s) in javascript RegExp?
e.g
regex = new RegExp("?????" , 'g');
I want to replace the emoticon :d, but only if it is surrounded by spaces (or at an end of the string).
Firstly, as Some1.Kill.The.DJ mentioned, I recommend you use the literal syntax to create the regular expression:
var pattern = /yourPatternHere/g;
It's shorter, easier to read and you avoid complications with escape sequences.
The reason why the pattern does not work is that JavaScript does not support lookbehinds ((?<=...). So you have to find a workaround for that. You won't get around including that character in your pattern:
var pattern = /(?:^|\s):d(?!\S)/g;
Since there is no use in capturing anything in your pattern anyway (because :d is fixed) you are probably only interested in the position of the match. That means, when you find a match, you will have to check whether the first character is a space character (or is not :). If that is the case you have to increment the position by 1. If you know that your input string can never start with a space, you can simply increment any found position if it is not 0.
Note that I simplified your lookahead a bit. That is actually the beauty of lookarounds that you do not have to distinguish between end-of-string and a certain character type. Just use the negative lookahead, and assure that there is no non-space character ahead.
Just for future reference that means you could have simplified your initial pattern to:
(?<!\S):d(?!\S)
(If you were using a regex engine that supports lookbehinds.)
EDIT:
After your comment on the other answer, it's actually a lot easier to use the workaround. Just write back the captured space-character:
string = string.replace(/(^|\s):d(?!\S)/g, "$1emoticonCode");
Where $1 refers to what was matched with (^|\s). I.e. if the match was at the beginning of the string $1 will be empty, and if there was a space before :d, then $1 will contian that space character.
Javascript doesnt support lookbehind i.e(?<=)..
It supports lookahead
Better use
/(?:^|\s)(:d)(?=$|\s)/g
Group1 captures required match

Issue with custom javascript regex

I have a custom regular expression which I use to detect whole numbers, fractions and floats.
var regEx = new RegExp("^((^[1-9]|(0\.)|(\.))([0-9]+)?((\s|\.)[0-9]+(/[0-9])?)?)$");
var quantity = 'd';
var matched = quantity.match(regEx);
alert(matched);
​
(The code is also found here: http://jsfiddle.net/aNb3L/ .)
The problem is that for a single letter it matches, and I can't figure out why. But for more letters it fails(which is good).
Disclaimer: I am new to regular expressions, although in http://gskinner.com/RegExr/ it doesn't match a single letter
It's easier to use straight regular expression syntax:
var regEx = /^((^[1-9]|(0\.)|(\.))([0-9]+)?((\s|\.)[0-9]+(\/[0-9])?)?)$/;
When you use the RegExp constructor, you have to double-up on the backslashes. As it is, your code only has single backslashes, so the \. subexpressions are being treated as . — and that's how single non-digit characters are slipping through.
Thus yours would also work this way:
var regEx = new RegExp("^((^[1-9]|(0\\.)|(\\.))([0-9]+)?((\\s|\\.)[0-9]+(/[0-9])?)?)$");
This happens because the string syntax also uses backslash as a quoting mechanism. When your regular expression is first parsed as a string constant, those backslashes are stripped out if you don't double them. When the string is then passed to the regular expression parser, they're gone.
The only time you really need to use the RegExp constructor is when you're building up the regular expression dynamically or when it's delivered to your code via JSON or something.
Well, for a whole number this would be your regex:
/^(0|[1-9]\d*)$/
Then you have to account for the possibility of a float:
/^(0|[1-9]\d*)(.\d+)?$/
Then you have to account for the possibility of a fraction:
/^(0|[1-9]\d*)((.\d+)|(\/[1-9]\d*)?$/
To me this regex is much easier to read than your original, but it's up to you of course.

How to build this a regex match in javascript

I have this string
[X=(any number)] (any character, include space) [/X]
Example: [X=5]Test string[/X]
I test with /(\[)(X=)(\d+\])(\w\s\.)(\[/X\])/gi but it doesn't work.
The problem is that the / needs to be escaped as well. Also it's not very useful to group constant matches. Lastly, you're matching (\w\s.) but this only allows one word character, one space character and one character of any type, in this order. I don't see what's the use of that. Try:
/\[X=(\d+)\](.+)\[\/X\]/gi
This will group the two variable parts.
Use this regex:
/\[X=\d+\].+\[\/X\]/gi
It checks if the given string is in the desired format.
var myregexp = /\[X=(\d+)\]([^[]*)\[\/X\]/g;
works for me, including if test string is multi-lines. Tested with RegexBuddy

Categories

Resources