0

I have below RegEx to validate a string..

var str = "Thebestthingsinlifearefree";
var patt = /[^0-9A-Za-z !\\#$%&()*+,\-.\/:;<=>?@\[\]^_`{|}~]*/g;
var res = patt.test(str);

the result will always give true but I thought it would give false.. because I checking any pattern which is not in the patt variable...

The given string is valid and it contains only Alphabets with capital and small case letters. Not sure what is wrong with the pattern.

5
  • What do you think this regex does? Commented Sep 13, 2016 at 7:38
  • * matches nothing, as well. Commented Sep 13, 2016 at 7:40
  • checking any occurrence of characters which is not included patt variable. Commented Sep 13, 2016 at 7:41
  • @prababuddy any feedback on the answers you received? Commented Sep 13, 2016 at 12:27
  • @ThomasAyoub, Sorry for delayed response... Its working fine but I have a scenario to capture non-keyboard characters(like copyright symbol or any other language characters), In that case, the regex pattern is failing... Ex.. if I entered "Ä/ä" this, the regex pattern is failing to catch this characters. any Idea on this... It will be great help to me.. Commented Sep 14, 2016 at 6:42

2 Answers 2

1

Here's your code:

var str = "Thebestthingsinlifearefree";
var patt = /[^0-9A-Za-z !\\#$%&()*+,\-.\/:;<=>?@\[\]^_`{|}~]*/g;
console.log(patt.test(str));

The regex

/[^0-9A-Za-z !\\#$%&()*+,\-.\/:;<=>?@\[\]^_`{|}~]*/g

will match anything since it accepts match of length 0 due to the quantifier *.

Just add anchors:

var str = "Thebestthingsinlifearefree";
var patt = /^[^0-9A-Za-z !\\#$%&()*+,\-.\/:;<=>?@\[\]^_`{|}~]*$/;
console.log(patt.test(str));


Here's an explanation or your regex:

[^0-9A-Za-z !\\#$%&()*+,\-.\/:;<=>?@\[\]^_`{|}~]* match a single character not present in the list below

    Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
    0-9 a single character in the range between 0 and 9
    A-Z a single character in the range between A and Z (case sensitive)
    a-z a single character in the range between a and z (case sensitive)
     ! a single character in the list  ! literally
    \\ matches the character \ literally
    #$%&()*+, a single character in the list #$%&()*+, literally (case sensitive)
    \- matches the character - literally
    . the literal character .
    \/ matches the character / literally
    :;<=>?@ a single character in the list :;<=>?@ literally (case sensitive)
    \[ matches the character [ literally
    \] matches the character ] literally
    ^_`{|}~ a single character in the list ^_`{|}~ literally
Sign up to request clarification or add additional context in comments.

4 Comments

Empty String would still pass the expression.
@WiktorStribiżew Thanks for sharing, I'll take a look, learn & correct my answer
Pretty sure OP does not want '' to pass the expression. Of course, I could be wrong. If not, change * to +.
0

Note that:

  • A search for a missing pattern is better expressed by a negative condition in code (!patt.test...).
  • You need to escape certain characters like ., (, ), ?, etc. by prefixing them with a backslash (\).

var str = "Thebestthingsinlifearefree";
var patt = /[0-9A-Za-z !\\#$%&\(\)*+,\-\.\/:;<=>\?@\[\]^_`\{|\}~]/;
var res = !patt.test(str);
console.log(res);

This will print false, as expected.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.