Frage

Will the match method in Javascript find a ^ caret character?

This is not working for me.

var theString = '^A^B^C^D';
var theMatch = theString.match(/^/g);

The ASCII code for the caret is 94. Can I match it by the ASCII code?

War es hilfreich?

Lösung

^ is a special character. You must escape it:

var theMatch = theString.match(/\^/g);

Andere Tipps

To complement @syntax excellent response. Please note that some characters are like "reserved keywords" in Regular expressions and any time you need to use them you will have to use \ followed by the character some other examples are \. \$ \[ \( and many others.

If need some additional help with regular expression I would like to recommend you a site that does an excellent job reading at your regular expression and this can help you understand them better:

http://regex101.com/

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top