This question already has answers here:
Regular expression, How to allow combination of dot (period) and letters?
(7 answers)
Closed 3 years ago.
from regexp=^(?:[a-zA-Z]+(?:[.'\-,])?\s?)+$, how am I suppose to do it allowing only alphanumeric and dots? Thanks!
Try this:
regexp = ^[a-zA-Z0-9\.]+$
This will get alphnum and dots..
/[a-zA-Z0-9.]/
In javascript the regular expressions shouldn't be in quotes, but in slashes. For exapmple:
var myregexp = /[a-z0-9\.]+/i;
var myvar = "I am a regular expression.";
var result = myregexp.test(myvar);
returns true, because in myvar there are only digits, letters and dots.
Note: the dots are special symbols and must be preceded by \
Related
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
Hi I'm trying to remove the letters and special characters from javascript.
Example:
var id = "Parameter[0].Category"
I only need the "0" from this. Thank you
Try this
var numbers = id.match(/\d+/)[0];
console.log(numbers);
In general this is called filtering using regular expressions.
If you want to filter out letters and special characters you can use regular expression in JS, like this:
id = id.replace(/\D/g, "")
You replace every (g option) character in your string that is not a digit \D with blank ""
This question already has answers here:
Regex created via new RegExp(myString) not working (backslashes)
(1 answer)
What is the difference between using "new RegExp" and using forward slash notation to create a regular expression?
(4 answers)
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I am trying to match all UK phone numbers in a string.
The pattern for this is:
^\(?(?:(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?\(?(?:0\)?[\s-]?\(?)?|0)(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}|\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4}|\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3})|\d{5}\)?[\s-]?\d{4,5}|8(?:00[\s-]?11[\s-]?11|45[\s-]?46[\s-]?4\d))(?:(?:[\s-]?(?:x|ext\.?\s?|\#)\d+)?)$
But when I try to initiate a new RegExp like:
const myRegex = RegExp('^\(?(?:(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?\(?(?:0\)?[\s-]?\(?)?|0)(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}|\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4}|\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3})|\d{5}\)?[\s-]?\d{4,5}|8(?:00[\s-]?11[\s-]?11|45[\s-]?46[\s-]?4\d))(?:(?:[\s-]?(?:x|ext\.?\s?|\#)\d+)?)$','g');
I get this error:
Uncaught SyntaxError: Invalid regular expression: /^(?(?:(?:0(?:0|11))?[s-]?(?|+)44)?[s-]?(?(?:0)?[s-]?(?)?|0)(?:d{2})?[s-]?d{4}[s-]?d{4}|d{3})?[s-]?d{3}[s-]?d{3,4}|d{4})?[s-]?(?:d{5}|d{3}[s-]?d{3})|d{5})?[s-]?d{4,5}|8(?:00[s-]?11[s-]?11|45[s-]?46[s-]?4d))(?:(?:[s-]?(?:x|ext.?s?|#)d+)?)$/: Invalid group
The error already shows you what the problem is. \ inside a string literal is the escape character. But escaping a character that doesn't need to be escaped simply "drops" the \:
console.log('\(');
So the value you are passing to the regular expression engine (and what the error shows you) is:
^(?(?...
(note: no backslash)
and (?( is not a valid character sequence in a regular expression.
You either have to
escape the escape characters ("\\(")
Use a regular expression literal instead of RegExp("string") (/^.../).
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).
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?
This question already has answers here:
Backslashes - Regular Expression - Javascript
(2 answers)
Closed 7 years ago.
regularExpression = '[\w]:\\.*';
function validate() {
debugger;
var regex = new RegExp(regularExpression);
var ctrl = document.getElementById('txtValue');
if (regex.test(ctrl.value)) {
return true;
} else {
return false;
}
}
Above is my code with which I am validating a directory path being entered. Here is the link for validator website which shows the perfect match for C:\ or D:\ or D:\Abcd\List.odt. But when I try to run the above JavaScript it fails.
When the Regular Expression string '[\w]:\\.*' is parsed by the JavaScript's RegEx engine, it will treat \w as an escaped character and since \w has no special meaning as an escaped character, it will treated as w only.
To fix this, you need to escape the \ like this
var regularExpression = '[\\w]:\\.*';
Or use RegEx literal like this
var regularExpression = /[\w]:\.*/;
As pointed out by Cyrbil in the comments, the character class [\w] is the same as \w only. So you can safely omit the square brackets around \w.