javascriptregexconsole.logjsfiddle

JavaScript string.match() doesn't work as expected


I am trying to match a string with regex as below. Jsfiddle returns the array with matched string as expected. But if I run the below statement in browser console, it returns null. I tried in ie11,Chrome,Mozilla.
Can anybody explain why this discrepancy?
Am I missing something.

"201458".match(/^20['^\s']{4,}$|^$/)

Solution

  • It should not match. You are looking for 20 followed by four or more of apostrophe, caret or a whitespace character (followed by a string end); 1 is none of those.

    Caret only has its special function when at start of the character class; apostrophe does not have any special function in a regexp.

    If you find that this matches in jsfiddle, please link the said fiddle; I will be very surprised.