【问题标题】:Find and replace all matching strings within textarea查找并替换 textarea 中的所有匹配字符串
【发布时间】:2012-02-07 05:06:25
【问题描述】:

我有这个

var textarea=$('#content'); 
textarea.html(textarea.html().replace("PID","111111")); 

这部分工作,但它只找到文本区域中的第一个“PID”并将其替换为“1111111”。还有大约 7 个人我也需要改变。我需要的是找到所有“PID”并将其替换为“111111”的方法。

提前致谢。

【问题讨论】:

    标签: jquery text replace area


    【解决方案1】:

    使用正则表达式替换字符串中的所有匹配项。试试这个

    textarea.html(textarea.html().replace(/PID/g,"111111")); 
    

    【讨论】:

      【解决方案2】:
      textarea.html(textarea.html().replace(new RegExp("PID","g"),"111111")); 
      

      “g”修饰符执行全局搜索。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-26
        • 2023-01-06
        • 1970-01-01
        • 2019-07-13
        • 1970-01-01
        • 2011-08-29
        • 2021-09-15
        • 2021-12-31
        相关资源
        最近更新 更多