在JavaScript中,使用String对象的replace方法时,会发现使用字符串直接量作为被替换对象时,只会替换第一个匹配的字符串,其余的匹配字符串保持不变。比如下面的代码,content中的第一个“营销”会被替换,但第二个不会:
var content = "营销管理系统,主要进行营销管理";
var replaced = content.replace("营销", "销售");
那么,要实现像Java中的String类的replaceAll那样进行全部替换要怎么做呢?答案是使用正则表达式进行字符串的全局替换。如下代码所示,实现了输入查找内容和替换后文本,然后执行全局替换。(注意使用了FreeMarker代码)
$("#replace_${comitem.comitemId}_${field.fieldId}").click(function(){
var content = $("#form${form.formId}_${field.fieldIdRow.sysName}").val();
if (content) {
var oldText = prompt("请输入查找内容:", "");
if (oldText) {
var newText = prompt("替换为:", oldText);
if (newText && newText != oldText) {
var replaced = content.replace(new RegExp(oldText, "g"), newText);
$("#form${form.formId}_${field.fieldIdRow.sysName}").val(replaced);
Eap.alert("已将【" + oldText + "】替换为【" + newText + "】");
}
}
}
});
你也可以看看下面这几篇文章:
(1) JS中实现replaceAll的方法(http://fuleonardo.iteye.com/blog/339749)
(2) JavaScript正则表达式:用String.replace()方法进行字符串替换
- 源文【JS正则表达式怎样实现Java中String.replaceAll的效果】最新版,请访问:
http://www.vktone.com/articles/js-regexp-replaceall.html
相关推荐
正则表达式主要配合以下方法使用 匹配 string.matches() 分割 string.split() 替换 string.replaceAll()
其中包括windows和linux文件路径的正则表达式;去除空格和斜杠转换的replaceall方法
RegexApplication/Default.aspx 正则表达式类的应用 RegexApplication/GetPageHtmlData.aspx 获取网页的内容 第10章(/10/) ASPNETValidator/Compare.aspx 比较验证 ASPNETValidator/...
大家都知道,正则表达式是一种可以用于模式匹配和替换的规范,一个正则表达式就是由普通的字符(例如字符a到z)以及特殊字符(元字符)组成的文字模式,它用以描述在查找文字主体时待匹配的一个或多个字符串。正则...
2) String replaceAll(String regex,String replacement):返回该字符串中所有匹配正则表达式的子串,然后替换成replacement指定的字符串。 3) String replaceFirst(String regex,String replacement);返回该字符...
string.prototype.replaceall 用于String.prototype.replaceAll的ES Proposal规范填充程序。 如果不可用或不String.prototype.replaceAll调用其“ shim”方法对String.prototype.replaceAll进行填充。 该软件包...
主要给大家介绍了关于String.replaceAll方法,正则妙用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
htmlStr =htmlStr.replaceAll("∀", "∀"); htmlStr =htmlStr.replaceAll("∂", "∂"); htmlStr =htmlStr.replaceAll("&exists;", "∃"); htmlStr =htmlStr.replaceAll("∅", "∅"); htmlStr...
Greediness(贪婪型):最大匹配 X?、X*、X+、X{n,} 是最大匹配。例如你要用 “<.+>” 去匹配 “a<tr>aava </tr>abb”,也许你所期待的结果是想匹配 “”,但是实际...System.out.println(test.replaceAll(reg, "##
主要给大家介绍了关于Java正则表达式如何匹配特定html标签内的内容的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
代码如下: public static void main(String[] args) { String sql = “SELECT * FROM \n” + ” `testdb`.`foo` LIMIT 0, 100″; String s = “SELECT * FROM `testdb`.`foo` LIMIT 0, 100”; String sql2 = Pattern...
JavaScript 中使用 replace 达到 replaceAll的效果,其实就用利用的正则的全局替换。
本文是对JS中实现replaceAll的方法进行了详细的总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助
JS 没有提供replaceAll这样的方法。使用正则表可以达成Replace 的效果,感兴趣的朋友看看下面的示例
当前,最常见的实现方法是使用全局正则表达式。 const queryString = 'q=query+string+parameters' ; const withSpaces = queryString . replace ( / \+ / g , ' ' ) ; 这种方法的缺点是要求转义特殊的RegExp字符...
文本正则表达式替换 使用text-icu正则表达式时易于替换。 安装 要安装它,只需使用 。 用法 OverloadedStrings所使用的语法与其他编程语言中的其他正则表达式库所使用的语法相似。 通常,输入文本被认为是静态的。...
您可能感兴趣的文章:C#将隐私信息(银行账户,身份证号码)中间部分特殊字符替换成*PHP的substr_replace将指定两位置之间的字符替换为*号PHP中利用substr_replace将指定两位置之间的字符替换为*号js正则
public static String arabNumToChineseRMB(int moneyNum){ String res=""; int i=3; if(moneyNum==0) return "零元"; while(moneyNum>0){ res=ChineseUnit[i++]+res; res=ChineseNum[moneyNum]+res; money...
js里没有replaceAll的函数,需要我们自定义 代码如下: String.prototype.replaceAll = function(s1,s2){ return this.replace(new RegExp(s1,”gm”),s2); } 2、使用js的replace进行处理 代码如下: str=str....
为包含正则表达式模式或可用标志的正则表达式对象。也可以是 String 对象或文字。如果 rgExp 不是正则表达式对象,它将被转换为字符串,并进行精确的查找;不要尝试将字符串转化为正则表达式。 replaceText 必选项...