javascript - What does / in the regular expression mean? -
javascript - What does / in the regular expression mean? -
i came across legacy code trying exact matches against strings using javascript regex.
i trying understand why using / before , after match string.
example
var match = thinger.match(/stringtomatch/);
what character do?
/regex here/
means of declaring regular look in javascript.
the /
character delimiter regular look declaration much single or double quote delimiter string declaration.
see mdn regular look reference page written description. regex can declared either of these 2 ways in javascript:
var re = /match string here/i; var re = new regexp("match string here", "i");
the advantages of using /regex here/
method are:
/
in reg when using method it's less typing , more compact the advantages of using new regexp("regex here")
method are:
new regexp("first" + foo + "whatever")
cannot other method. you don't have worry escaping forwards slashes javascript regex
Comments
Post a Comment