Search code examples
javascriptstring-length

Length property not returning the desired value


I tried to get the length of this string:

$args[0]-match'\d.*?\/(.*)';$matches[1]

using:

console.log("$args[0]-match'\d.*?\/(.*)';$matches[1]".length);

I did this in the browser console. It returns 37. However, counting by hand, this string is 39 characters long. Am I missing something or is it a bug in the browser?


Solution

  • backslash character \ is a special escape character in strings so it doesn't count.

    you can make backslashes count by preceding them with another backslash (that is escape the escape character):

    console.log("$args[0]-match'\\d.*?\\/(.*)';$matches[1]".length)