javascript regex for american phone number [duplicate] - javascript

This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 8 years ago.
I have this regex expression var re = /(?:\d{3}|\(\d{3}\))([\w-\/\.]?)\d{3}\1\d{4}/;, however, the \w whitespace doesn't work on this test console.log(re.test('123 456 7890'));
Here is my jsfiddle: http://jsfiddle.net/Bqb22/

You shouldn't use \w for whitespace. Use \s instead. (\w is word character, the same as [0-9A-Za-z_], and should not be used to indicate whitespace).

Related

Regex pattern for URL string [duplicate]

This question already has answers here:
Regex to match only letters
(20 answers)
Closed 3 years ago.
I need to find regex pattern for url and use regex.test() so only string like this:
http://*.margonem.pl/
so it's exactly like above string and where * must appear and can be string which only contains a-z letters without any signs.
That would be http:\/\/[a-z]+\.margonem\.pl\/:
Matches
http://a.margonem.pl/
http://foo.margonem.pl/
Does not match
http://hello-world.margonem.pl/
http://abcq443435u4531.margonem.pl/

Use regex backreference inside character except class [^] [duplicate]

This question already has answers here:
Negating a backreference in Regular Expressions
(6 answers)
Closed 4 years ago.
Lets take:
stringi = 'xnxx xnnx xnnxn'
My regex is: (n)[^n]
I want to make my regex a little more dynamic like that:
(n)[^\1] -\1 beeing the capt. grp 1
My desired result would be that:
(n)[^\1] would be equal (n)[^n]
(x)[^\1] would be equal (x)[^x]
How can I not match a NOT-\1 character?
using a negative lookahead, the . is to match any character as n length is one
(n)(?!\1).

Regex in JavaScript acting differently than elsewhere [duplicate]

This question already has answers here:
How can I match a whole word in JavaScript?
(4 answers)
Closed 5 years ago.
I am trying to match the word ethane (preceded with nothing) while avoiding methane. I've tried this in an online regex tester: /(?<!m)ethane/i (which works), but I get an invalid regex expression error in JavaScript. What am I doing wrong?
You can use RegExp /\bethane\b/ to match "ethane" and not "methane"
var thanes = ["ethane", "methane"];
var re = /\bethane\b/;
thanes.forEach(word => console.log(re.test(word)));
See
Difference between \b and \B in regex
How does \b work when using regular expressions?

Match alphanumeric string with length six chars using regex [duplicate]

This question already has answers here:
Regular expression to limit number of characters to 10
(6 answers)
Closed 6 years ago.
I need regex for validating alphanumeric String with length of 6 chars. I tried with following regex And done it for allowing alphanumeric chars but don't know how to stop exceeding more than six chars.
var regex = new RegExp("^[a-zA-Z0-9\b]+$");
Fiddle here.
you just have to add {6} in the end
var regex = new RegExp('[a-zA-Z0-9\b]{6}$'); You can test it out here https://regex101.com/

How to match the character class [:alpha:] in JavaScript [duplicate]

This question already has answers here:
How can I use Unicode-aware regular expressions in JavaScript?
(11 answers)
Regular expression to match non-ASCII characters?
(8 answers)
Closed 8 years ago.
I need to match all alphabetic characters (not only [a-zA-Z], but really all of them including ö, ß, â, ç, Æ, Å, Ĺ, Ĩ, Ÿ, Ș, њ, ѝ, Ц, ت, ר). In other programming languages there is character class named [:alpha:] for this, because it is virtually impossible to name all alpha characters from all alphabets in brackets.
\w doesn't help because it includes digits and underscore. I need letters only without digits, punctuation, spaces.
If you can use XRegExp, use. It has support for Unicode. Otherwise you have to enumerate the ranges yourself.

Categories