+ 2
Please tell why this isn't working
var value = "1 (555) 555 5555"; // checks if value contains any brackets. Returns true if not console.log(/[^()]/.test(value)) Why doesn't it return false? It returns true. Please check
14 Réponses
+ 4
sajid chowdhury Ah... I see the issue.
Even though parentheses exist in "1 (555) 555 6555", the pattern /[^()]/ matches as true because it's actually checking that there is at least one character that isn't a parenthesis.
Therefore, to check that none of the characters are parentheses, the pattern would need to check every character from start to finish as seen in this pattern: /^[^()]+$/
I've updated the code to include this line to show this in action.
--------
test("Test for NO parentheses:",
/^[^()]+$/, values)
----
Or you can test this with your version of the code:
--------
var nums= "1 (555) 555 6555";
console.log(/^[^()]+$/.test(nums))
----
+ 6
sajid chowdhury Your updated code should work. You don't need to escape the parentheses when contained within the brackets.
The original regex was missing the character class brackets [].
That said, I put together some additional variations for you to review. Hopefully, these will be helpful.
https://code.sololearn.com/WyTMcT7M6XFg/
+ 5
It looks like David has taking under his wing, you should be squared away now. I still suggest you check out regexr.com it will help you grain better understanding of regexp.
+ 3
This (/^()/) is not checking if value contains parens. As is you are checking for any of the patterns specified within the parens (), which is nothing. You are testing for nothing at the beginning of string value. Hence the reason the return is true because nothing is at the beginning of string value.
With snippet as-is, you need to escape the parens (/^\(\)/). But this just tests if the string starts with empty parentheses.
https://www.w3schools.com/jsref/jsref_obj_regexp.asp
https://code.sololearn.com/WUxFIpDnZf8O/#js
+ 2
sajid chowdhury
The latest change /[^\(\)]/ it appears that it will work,
Testing to match every character that is not an opening or closing paren.
Is that what you want?
You should check out this site to help
regexr.com/536ls
+ 2
sajid chowdhury I'm glad that code was helpful. I've cleaned it up a bit if you want to review it again.
+ 1
🔫 Rick Grimes why can't I use a shorter code? Like the one above?
+ 1
David Carroll thanks a lot Sir, for replying
+ 1
David Carroll shouldn't the test return false if there are any brackets? But it isnt doing that for me.
var nums= "1 (555) 555 6555";
console.log(/[^()]/.test(nums))
this returns true which it shouldn't.
pls check a bit.
I'm also going to update code above
+ 1
David Carroll works perfectly. Thanks a lot.
0
If you want to check for any type of bracket, you need to check for all of it.
I don't know if this is best way to do it but this will do your job.
console.log(!/[(,),{,},<,>,\[,\]]/.test(value))
0
ODLNT I've changed my code. Please check now
0
ODLNT I basically want to check if there aren't any parentheses. If there are ,3 digits should be between them. Having a hard time,just to solve this little problem. Now hopefully mr mods answer would be correct
0
Your regex find the pattern that initiate with a parentheses ( and the next character is the other parentheses ).
Your regex might be /[^(.)]/