在javascript中搜索和替换特定的查询字符串参数值 [英] Search and replace specific query string parameter value in javascript

查看:141
本文介绍了在javascript中搜索和替换特定的查询字符串参数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的字符串:

I have a string which is something like this :

a_href= "www.google.com/test_ref=abc";

我需要在上面的strinng中搜索test_ref = abc,并用新值替换

I need to search for test_ref=abc in thisabove strinng and replace it with new value

var updated_test_ref = "xyz";

a_href ="www.google.com/test_ref=updated_test_ref" 

www.google.com/test_ref=xyz.

我们该怎么做?

编辑:

test_ref值本身可以是URL链接,如 http://google.com?param1=test1&param2=test2 。我需要捕捉完整的价值,直到第一&

test_ref value can be a URL link in itself something like http://google.com?param1=test1&param2=test2. I need to capture complete value not till first &.

推荐答案

a_href = a_href.replace(/(test_ref=)[^\&]+/, '$1' + updated_test_ref);

这篇关于在javascript中搜索和替换特定的查询字符串参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆