0
/\w+/.test('@asd') // true
/\w+/.test('@') // false
/\w+/.test('asd') // true

Shouldn't the first one return false as well? consider there is a special character inside the string.

I did read thought the doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test probably missed something important

The test() method executes a search for a match between a regular expression and a specified string. Returns true or false.

please correct me on which part am i missing here to not getting the expected result.

6
  • 1
    asd inside the string matches, so no, the match doesn't fail. Your third line is incorrect too, /\w+/.test('asd') // false gives true Commented Mar 31, 2020 at 0:57
  • Thanks @CertainPerformance how can i made it search the entire string and return false. Commented Mar 31, 2020 at 0:58
  • 1
    @Bill Anchor it. /^\w+$/.test('@asd') Commented Mar 31, 2020 at 0:59
  • thanks @mpen oh i see, i guess you will have to do that way, is there another JS api which will do that? match? Commented Mar 31, 2020 at 1:00
  • 1
    @Bill No, there aren't any built-in methods that add the ^ and $ as far as I'm aware. You can write one, but it's not quite as pretty. Commented Mar 31, 2020 at 1:03

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.