Regex doesn't check for end of line

Consider the following scenario (Javascript code):

regex = new RegExp((/([\d,.]+)[ $]/));
value = "2.879"  

      

Regexp does not match value but does match (value + ""), so I think $ is not the same? Why is this?

Shouldn't $ be checking for end of line?

+3


source to share


1 answer


Special characters such as $

do not have the same meaning within a character class. In a character class, they are just characters, so [ $]

will match a space character or a character $

. It won't match the end of the line.



If you want to match a space character or the end of a line, you must use alternation, i.e. ( |$)

...

+6


source







All Articles