Javascript Regular Expression to match six-digit number [duplicate] - javascript

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I am trying to incorporate a regular expression i have used in the past in a different manner into some validation checking through JavaScript.
The following is my script:
var regOrderNo = new RegExp("\d{6}");
var order_no = $("input[name='txtordernumber']").val();
alert(regOrderNo.test(order_no));
Why would this not come back with true if the txtordernumber text box value was a six digit number or more?

You have to escape your \ when used inside a string.
new RegExp("\\d{6}");
or
/\d{6}/

Insert an extra "\" in your regexp.

You need to escape your backslash. It's looking for "\d", not digits.
So...
var regOrderNo = new RegExp("\\d{6}");

Related

invalid Regex group [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I'm trying to create the following regex using Javascript.
(?<!\\)(?:\\{2})*\\(?!\\)([5-9]|[1-9]\d)
However, by doing this it gives me invalid group error in the console.
regExp = new RegExp("(?<!\\)(?:\\{2})*\\(?!\\)([5-9]|[1-9]\d)", "gi");
I don't understand where the problem comes from exactly. I appreciate the help.
Thank you
EDIT: After some research I found that Javascript does not support lookbehinds.
So the error comes from (?<!\\).
Refer this newly asked question to find an alternative way to do the same job.
How to check for odd numbers of backslashes in a regex using Javascript?
If your expression isn't dynamic, just use a literal:
var regExp = /(?<!\\)(?:\\{2})*\\(?!\\)([5-9]|[1-9]\d)/gi;
The problem is that your escape sequences \\ inside the string end up rendering \ characters inside the regEx, which in turn end up escaping brackets they shouldn't, resulting in unterminated groups.

same dynamic regex and inline regex not giving same output in javascript [duplicate]

This question already has answers here:
Backslashes - Regular Expression - Javascript
(2 answers)
Closed 7 years ago.
I have been staring at these two flavors of same regex and can't figure out why the outcome is different:
var projectName="SAMPLE_PROJECT",
fileName="1234_SAMPLE_PROJECT",
re1 = new RegExp('^(\d+)_SAMPLE_PROJECT$','gi'),
re2 = /^(\d+)_SAMPLE_PROJECT$/gi,
matches1 = re1.exec(fileName),
matches2 = re2.exec(fileName);
console.log(matches1);//returns null
console.log(matches2);//returns correctly
Here is the jsbin : https://jsbin.com/badoqokumu/edit?html,js,output
Any idea what I must be doing wrong with instantiating RegExp?
Thanks.
In the first case, you have a string literal, which uses \ to introduce escape sequences. \d in a string is just d. If you want \d, you need to type \\d instead.
In the second case, you have a regular expression literal, which does not interpret \ as a string escape sequence.

Wanted to check whether string ends with special character or not? [duplicate]

This question already has an answer here:
Need to do a right trim on ajax query in javascript?
(1 answer)
Closed 8 years ago.
I wanted to check whether my string which ends with special character or not. If my string contains special character at the end, then need to trim at the right. if not, do nothing.
My piece of code:
var s = 'acbd#';
var x = 'abcd#e'
Expected Result:
acbd
abcd#e
any help on this?
You can use regular expression to replace the special characters with empty string, like this:
s.replace(/[#]+$/, "");
x.replace(/[#]+$/, "");
You can specify more special characters inside of square brackets.

Javascript regex to extract the string before the last backslash [duplicate]

This question already has answers here:
Regex for everything before last forward or backward slash
(3 answers)
Closed 9 years ago.
I am dealing with timezone's in Javascript and I need a regex that will extract everything, but the timezone name from it. For example, I have the timezone America/Argentina/Buenos_Aires. I want to extract the America/Argentina part with a regex. Currently I have this regex: tz.match(/.*?(?=\/|$)/i)[0] which extracts everything to the first backslash which works for most timezones (America/Los_Angeles), but not for all of them. How could I edit that regex so that it gets the string before the last value?
I'd personally suggest avoiding regular expressions for something like this, when simple string functions/methods would suffice admirably:
var stringVariable = 'America/Argentina/Buenos_Aires',
text = stringVariable.substring(0, stringVariable.lastIndexOf('/'));

Matching content within a string with one regex [duplicate]

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Simple way to use variables in regex
(3 answers)
Closed 10 years ago.
I am looking for a way to RegEx match for a string (double quote followed by one or more letter, digit, or space followed by another double quote). For example, if the input was var s = "\"this is a string\"", I would like to create a RegEx to match this string and produce a result of [""this is a string""].
Thank you!
Use the RegExp constructor function.
var s = "this is a string";
var re = new RegExp(s);
Note that you may need to quote the input string.
This should do what you need.
s =~ /"[^"]*"/
The regex matches a double quote, followed by some number of non-quotes, followed by a quote. You'll run into problems if your string has a quote in it, like this:
var s = "\"I love you,\" she said"
Then you'll need something a bit more complicated like this:
s =~ /"([^"]|\\")*"/
I just needed a pattern to match a double quote followed by one or more characters(letters, digits, spaces) followed by another double quote so this did it for me:
/"[^"]*"/

Categories

Resources