javascript中使用replaceAll()函数实现字符替换的方法_javascript技巧

而str.replace(/\-/g,"!")则可以全部替换掉匹配的字符(g为全局标志)。
replace()
The replace() method returns the string that results when you replace text matching its first argument
(a regular expression) with the text of the second argument (a string).
If the g (global) flag is not set in the regular expression declaration, this method replaces only the first
occurrence of the pattern. For example,
var s = "Hello. Regexps are fun." ;s = s.replace(/\./, "!" ); // replace first period with an exclamation pointalert(s);
produces the string “Hello! Regexps are fun.” Including the g flag will cause the interpreter to
perform a global replace, finding and replacing every matching substring. For example,
var s = "Hello. Regexps are fun." ;s = s.replace(/\./g, "!" ); // replace all periods with exclamation pointsalert(s);
yields this result: “Hello! Regexps are fun!”
所以可以用以下几种方式.:
string.replace(/reallyDo/g, replaceWith);
string.replace(new RegExp(reallyDo, 'g'), replaceWith);
string:字符串表达式包含要替代的子字符串。
reallyDo:被搜索的子字符串。
replaceWith:用于替换的子字符串。

复制代码 代码如下:

<script type="text/javascript">
  String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) {
  if (!RegExp.prototype.isPrototypeOf(reallyDo)) {
return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi": "g")), replaceWith);
} else {
return this.replace(reallyDo, replaceWith);
}
}
</script>

时间: 2024-07-29 04:33:19

javascript中使用replaceAll()函数实现字符替换的方法_javascript技巧的相关文章

JavaScript中利用Array和Object实现Map的方法_javascript技巧

本文实例讲述了JavaScript中利用Array和Object实现Map的方法.分享给大家供大家参考.具体如下: 昨天突然看到以前别人用JavaScript实现的Map感觉很不错,但是发现有个别方法有问题,顺便完善了下,添加了 remove .indexOf .values.clear等方法. /** * @author blune68 * @version 0.1, 07/27/12 * */ function Map(){ this.keys = new Array(); this.dat

JavaScript中使用ActiveXObject操作本地文件夹的方法_javascript技巧

在Windows平台上, js可以调用很多Windows提供的ActivexObject,本文就使用js来实现文档处理, 和使用js编写ActiveX做一个简单介绍. 复制代码 代码如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <t

JavaScript中获取HTML元素值的三种方法_javascript技巧

JavaScript中取得元素的方法有三种:分别是: 1.getElementById() 方法:通过id取得HTML元素. 2.getElementsByName()方法:通过name取得元素,是一个数组. 3.getElementsByTagName()方法:通过HTML标签取得元素,是一个数组. 如果要取得值可以使用value,如:var x=document.getElementById("id").value; 方法一:getElementById() 方法可返回对拥有指定

在Javascript中为String对象添加trim,ltrim,rtrim方法_javascript技巧

以下我们就用这个属性来为String对象添加三个方法:Trim,LTrim,RTrim(作用和VbScript中的同名函数一样) 复制代码 代码如下: String.prototype.Trim = function() {     return this.replace(/(^\s*)|(\s*$)/g, ""); } String.prototype.LTrim = function() {     return this.replace(/(^\s*)/g, "&quo

JavaScript中通过提示框跳转页面的方法_javascript技巧

通过提示框跳转页面具体代码如下所示: <!doctype html> <html lang="en"> <head> <meta charset="UTF-"> <title>Document</title> </head> <body> <script> window.onload = function(){ //设置当页面加载时执行 var btn =do

JavaScript中字符串(string)转json的2种方法_javascript技巧

第一种方式: 使用js函数eval(); testJson=eval(testJson);是错误的转换方式. 正确的转换方式需要加(): testJson = eval("(" + testJson + ")"); eval()的速度非常快,但是他可以编译以及执行任何javaScript程序,所以会存在安全问题.在使用eval().来源必须是值得信赖的.需要使用更安全的json解析器.在服务器不严格的编码在json或者如果不严格验证的输入,就有可能提供无效的json

JavaScript中实现异步编程模式的4种方法_javascript技巧

你可能知道,Javascript语言的执行环境是"单线程"(single thread). 所谓"单线程",就是指一次只能完成一件任务.如果有多个任务,就必须排队,前面一个任务完成,再执行后面一个任务,以此类推. 这种模式的好处是实现起来比较简单,执行环境相对单纯:坏处是只要有一个任务耗时很长,后面的任务都必须排队等着,会拖延整个程序的执行.常见的浏览器无响应(假死),往往就是因为某一段Javascript代码长时间运行(比如死循环),导致整个页面卡在这个地方,其他

javascript中String类的subString()方法和slice()方法_javascript技巧

在该书2.8.4节中讲到String类中的subString()方法和slice()方法,其用法和返回结果都基本相同,如下示例: 复制代码 代码如下: var strObj = new String("hello world"); alert(strObj.slice(3)); // 输出结果:"ol world" alert(strObj.subString(3)); // 输出结果:"ol world" alert(strObj.slice(

JavaScript中实现最高效的数组乱序方法_javascript技巧

数组乱序的意思是,把数组内的所有元素排列顺序打乱. 常用的办法是给数组原生的sort方法传入一个函数,此函数随机返回1或-1,达到随机排列数组元素的目的. 复制代码 代码如下: arr.sort(function(a,b){ return Math.random()>.5 ? -1 : 1;}); 这种方法虽直观,但效率并不高,经我测试,打乱10000个元素的数组,所用时间大概在35ms上下(firefox) 本人一直具有打破沙锅问到底的优良品质,于是搜索到了一个高效的方法.原文见此 复制代码