JS Replace 全部替换字符 用法
来源:互联网 发布:校园网络交流平台 编辑:程序博客网 时间:2024/05/02 00:43
最近工作中遇到跳回原URL的问题,并且在URL中包含多参,这里需要对特殊字符(%、&、-、+、= ...)进行转义
% %25
& %26
+ %2B
/ %2F
# %23
= %3D
空格 %20
e.g
str.replace(rgExp, replaceText)
参数
str
必选项。要执行该替换的 String 对象或字符串文字。该字符串不会被 replace 方法修改。
rgExp
必选项。为包含正则表达式模式或可用标志的正则表达式对象。也可以是 String 对象或文字。如果 rgExp 不是正则表达式对象,它将被转换为字符串,并进行精确的查找;不要尝试将字符串转化为正则表达式。
replaceText
必选项。是一个String 对象或字符串文字,对于stringObj 中每个匹配 rgExp 中的位置都用该对象所包含的文字加以替换。在 Jscript 5.5 或更新版本中,replaceText 参数也可以是返回替换文本的函数。
在这里需要替换的内容为正则表达式,"/g"表示全文匹配。
演示代码:
原URL:http://club.womai.com/forum.php?mod=post&action=newthread&fid=2&productid=69103
function login(){
var url = location.href;
var newUrl = url.replace(/&/g, "%26");
window.location = "http://www.womai.com/Member/LoginForm.htm?returnUrl=" + newUrl;
}
这样就可以完成全字符串的搜索并跳回正确的URL。
- JS Replace 全部替换字符 用法
- JS Replace 全部替换字符 用法
- JS Replace 全部替换字符 用法
- JS Replace 全部替换字符用法
- jS Replace 全部替换字符 用法
- JS Replace 全部替换字符 用法
- JS Replace 全部替换字符
- JS Replace全部替换字符方法
- js replace 全部替换
- js replace 替换全部
- js replace 全部替换字符串
- Jquery,JS全部替换replace
- jquery 将某个字符全部替换 replace
- JS 字符串全部替换 ,replace (/ /g,'')
- JS RegExp和replace全部替换
- js中 replace() 全部替换实例
- js 全部替换replace() 使用正则表达式
- js中 replace replaceAll 全部替换
- iphone开发之轻松搞定原生socket 编程,阻塞与非阻塞,收发自如
- Servlet容器的类加载器加载顺序
- 个人总结.
- 提问的智慧
- cudaSafeCall() Runtime API error : unspecified driver error
- JS Replace 全部替换字符 用法
- 打开IE时,弹出对话窗口,提示“您计算机上某个程序已损坏了您对 Internet Explorer 默认搜索提供程序的设置”
- vc的一些文件后缀
- 正则习点 --- 09
- windows 命令大全
- 链接错误问题原因定位
- 线性代数学习框架
- eclipse、myeclipse使用常用的小技巧(eclipse alt+/ 自动补全 )
- C++虚函数