javascript – How do I write "or" in a regular expression?


We have the line url = 'http://site.loc/?a=1&search=abcdf';
and the regular expression re = /\&search\=.*?(?=\&)/g;

Only it works if it finds the & symbol behind the search word.
If it is not there, then the expression does not work.

How do I fix the regex to execute either before & or the end of the $ string?


Try it. Symbol | – this or, also for such searches is used [^ &] + – any number greater than 1, except &

Scroll to Top