Matching list symbols in regex (angular 2) [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 match the list of symbols in regex but somehow the result is always returning with errors
symbol list = !##$+*{}?<>&’”[]=%^
if (text.match('^[\[\]\!\"\#\$\%\&\'\(\)\*\+\,\/\<\>\=\?\#\[\]\{\}\\\\\^\_\`\~]+$')) {
this.specialChar = true;
} else {
this.specialChar = false;
}
I am getting the following error:
Invalid regular expression: /^[[]!"#$%&'()*+,/<>=?#[]{}\\^_`~]+$/: Nothing to repeat
How do I correctly match the symbols in regex? basically I want to check if text contain any of those symbols.

You should use this regex constructor instead:
if (text.match(/^[\[\]\!\"\#\$\%\&\'\(\)\*\+\,\/\<\>\=\?\#\[\]\{\}\\\\\^\_\`\~]+$/)) {
this.specialChar = true;
} else {
this.specialChar = false;
}
The reason it fails is that you use a regex string constructor. If you still want to do that, you need to DOUBLE escape the characters, like this:
if (text.match('^[\\[\\]\\!\\"\\#\\$\\%\\&\\'\\(\\)\\*\\+\\,\\/\\<\\>\\=\\?\\#\\[\\]\\{\\}\\\\\\^\\_\\`\\~]+$')) {
Now you will create a valid regex.

Related

How can I disallow a square bracket in a string when using regex to validate input text [duplicate]

This question already has answers here:
Difference between regex [A-z] and [a-zA-Z]
(6 answers)
Closed last month.
I want to disallow a string that has square brackets when using test method in regex.
function validateName(name){
var nameRegex = /^[A-zA-z\s][A-zA-z\s.-]{1,64}$/i;
console.log('##########Checking validation.........');
return nameRegex.test(name);
}
Try this regex and let me know:
/^[^\[\]]*$/
EDIT:
const regex = /^[^\[\]]*$/;
console.log(regex.test('ok')) // return true
console.log(regex.test('[ok')) // return false
console.log(regex.test('ok]')) // return false

String split with different separators JavaScript [duplicate]

This question already has answers here:
Including a hyphen in a regex character bracket?
(6 answers)
Closed 3 years ago.
I am asking you how to split a string using different separators and when the string is empty return just an empty space.
I don't know to combine both.
All I have is:
function split(string) {
var str = string.split(/[+-*]/);
return str;
}
Example:
split("este-es+otro*ejemplo"); // => ["este", "es", "otro", "ejemplo"]
split(''); // => [""]
Thank you.
Move the * at the first position inside square bracket ([]).
If any special character, such as backslash (*) is immediately after the left square bracket, it doesn't have its special meaning and is considered to be one of the characters to match literally.
Try /[*+-]/g
function split(string) {
var str = string.split(/[*+-]/g);
return str;
}
console.log(split("este-es+otro*ejemplo"));

3 Character Long Alphanumeric Regex Not Working [duplicate]

This question already has answers here:
How to detect exact length in regex
(8 answers)
Closed 4 years ago.
So I am trying to use a regular expression to check against strings but it doesn't seem to be working properly.
Basically I want it to match a alpha-numeric string that is exactly 3 characters long. The expression I am using below does not seem to be working for this:
const msg = message.content;
const regex = /[A-Za-z0-9]{3}/g;
if (msg.match(regex)) {
// Do something
}
Am I doing something wrong? Any help would be appreciated. Thanks in advance.
You need to add ^ and $ for the start-of-string anchor and end-of-string anchor, respectively - otherwise, for example, for #123, the 123 will match, and it will pass the regex. You also might consider using the i flag rather than repeat A-Za-z, and you can use \d instead of 0-9.
It looks like you just want to check whether the string passes the regex's test or not, in which case .test (evaluates to a boolean) might be a bit more appropriate than .match. Also, either way, there's no need for the global flag if you're just checking whether a string passes a regex:
const regex = /^[a-z\d]{3}$/i;
if (regex.test(msg)) {
// do something
}

js regexp - to escape character, needs one backward slash? or two? [duplicate]

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
How to properly escape characters in regexp
(4 answers)
Closed 4 years ago.
I can't understand when I need one and two backward slashes \.
First, see an example:
const rxHttps = new RegExp("\/");
console.log(rxHttps.test("/")); // true
const rxQuestion = new RegExp("\\?");
console.log(rxQuestion.test("?")); // true
const rxAnotherQuestion = new RegExp("\?"); // Uncaught SyntaxError: Invalid regular expression: /?/: Nothing to repeat
In the above example, to use a character /, it needs just one \.
However, ? needs two \, or SyntaxError occurs.
This is really confusing. I can't make heads or tails of it.
Why are they different? Am I misunderstanding?
because '?' is used to mean that what's before it can or not exist like {0,}
for example
var reg = new RegExp('a?');
// this has the same meaning as using
// var reg = new RegExp('a\?');
reg.test('a'); // will display true
reg.test('anything'); // this will also display true
reg.test('123'); // this also displays true
so if you want to check of the existence of '?' character, you have to escape it, and since it's used as '\?' also, then you have to escape it using '\?'
Why does it give you syntax error ?
because it's expecting something to check if it exists or not and you're not giving it anything.

Javascript Regex For only numbers and no white spaces [duplicate]

This question already has an answer here:
Why this javascript regex doesn't work?
(1 answer)
Closed 2 years ago.
I was very surprised that I didn't find this already on the internet.
is there's a regular expression that validates only digits in a string including those starting with 0 and not white spaces
here's the example I'm using
function ValidateNumber() {
var regExp = new RegExp("/^\d+$/");
var strNumber = "010099914934";
var isValid = regExp.test(strNumber);
return isValid;
}
but still the isValid value is set to false
You could use /^\d+$/.
That means:
^ string start
\d+ a digit, once or more times
$ string end
This way you force the match to only numbers from start to end of that string.
Example here: https://regex101.com/r/jP4sN1/1
jsFiddle here: https://jsfiddle.net/gvqzknwk/
Note:
If you are using the RegExp constructor you need to double escape the \ in the \d selector, so your string passed to the RegExp constructor must be "^\\d+$".
So your function could be:
function ValidateNumber(strNumber) {
var regExp = new RegExp("^\\d+$");
var isValid = regExp.test(strNumber); // or just: /^\d+$/.test(strNumber);
return isValid;
}

Categories