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:

you can freely utilize quotes in match without having escape them, though have escape / in reg when using method it's less typing , more compact

the advantages of using new regexp("regex here") method are:

you can utilize javascript expressions dynamically build string or regex arguments in new regexp("first" + foo + "whatever") cannot other method. you don't have worry escaping forwards slashes

javascript regex

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -