2

I would appreciate if someone can help me to come up with a regex that I can look for a pattern in a href. Pattern is to look for a query string hint=value& and then replace it with a new value hint=value2&. so the pattern should start with hint and end with & if there are more query strings or end of the value for hint.

I dont want to use jquery external library (purl). Any help will be much appreciated.

4
  • So why is str.replace("hint=value&", "hint=value2&") not enough? Commented Apr 2, 2017 at 5:07
  • I dont know the "value", it is one of the query string and the value can be anything Commented Apr 2, 2017 at 5:14
  • Right. But how do you know value2? Could you give some examples, on a string you want to match, and what you want to replace. Commented Apr 2, 2017 at 5:15
  • Here is the example of the url: sample.com/…" what I want is a regex to find hint=m%2A%2A%2A%2A%2A%2A%2Ai%40gmail.com and replace it with hint=newstring Commented Apr 2, 2017 at 5:18

2 Answers 2

2

You could use a positive lookahead and check for & or the end of the string.

hint=(.*?)(?=&|$)

Live preview

Since we're using a lookahead, this means that the replacement doesn't need to include the & at the end. Which might be an important factor if hint=value were to be the last query element.

Which in JavaScript would look like this:

const str = "https://www.sample.com/signup?es=click&hint=m%2A%2A%2A%2A%2A%2A%2Ai%40gmail.com&ru=%2F%22";

const replacement = "hint=newstring";

const regex = /hint=(.*?)(?=&|$)/g;

const result = str.replace(regex, replacement);

console.log(result);

Given your example url, then console.log(result) would output:

https://www.sample.com/signup?es=click&hint=newstring&ru=%2F%22
Sign up to request clarification or add additional context in comments.

3 Comments

thank you, it works. can you help me understand why different regex are working and which one is the accurate one? I am not good with regex at all :(
Take a look at the live preview, there's an in-depth explanation. However (.*?)& works, but it wouldn't work if hint=value is the last element in the query. Then you could do (.*?)(&|$) however now you need to add & to the end of the replacement if needed. This is why a lookahead (?=&|$) is best. Because here it just checks for the & but never captures it. Thus only replaces what is interesting to you, so to speak.
Thanks a lot Vallentin
0

Snippet:

function replaceValue(newValue, url) {
    const regex = /\?.*?&((hint)=(.*)?&(.*))/g;
    const matches = regex.exec(url);
    let result = '';
    matches.forEach((matchString , index) => {
        if(index === 3) {
            result += newValue;
        }
        else {
            result += matchString;
        }
    });
    return result;
}

This would help you

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.