1

I found this regular expression in some javascript code and not sure exactly what is doing to the string.

name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); var regexS = "[\\?&]"+name+"=([^&#]*)"; var regex = new RegExp( regexS ); var state = History.getState(); var current_url = state.cleanUrl; var results = regex.exec( current_url ); 

2 Answers 2

1

This regex is used to see if the parameter contained in name (ie. param or param[]) exists in the url current_url.

Examples, for name the regex is:

name regex ---- ----- param [?&]param=[^&#]* param[] [?&]param\[\]=[^&#]* 

The regex will search if there is a ? or a # followed by param= followed by any number of character that is not & or # (ie. the value of the param).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks M42 this is also helpful
0

First, it replaces [ with \[ and ] with \] in name. Then it puts name in a new regex which searches for \, ? or & followed by the name variable, followed by =, followed by any number of ^, &, or # characters.

In the context of a url, it seems it means to find a particular $_GET variable.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.