Search code examples
javascriptregexstringreplacedigits

How may I replace word and digit combination with blank?


How to replace

url : http://www.abc.com&query_id=123&query_id1=12

to : http://www.abc.com&query_id1=12

How to replace any query_id with blank in javascript ?


Solution

  • var url = "http://www.abc.com&query_id=123&query_id1=12";
        url = url.replace(/(\?|&)query_id=[^&]*/g, "");
    
    console.log(url); /* returns http://www.abc.com&query_id1=12 */
    

    anyway note that you should replace the first occurence of & with a ?

    http://www.abc.com&query_id=123&query_id1=12

    should be written as

    http://www.abc.com?query_id=123&query_id1=12