Question:
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?
Answer:
Try it. Symbol | – this or, also for such searches is used [^ &] + – any number greater than 1, except &
console.log('http://site.loc/?search=abcdf&a=1'.match(/(\?|&)search=([^&]+)/)[2])