【发布时间】:2012-12-12 19:37:52
【问题描述】:
我想搜索一个不区分大小写的字符串,但是用原始字符串的大小写字母更改子字符串。
var mystring = "FiRst Last";
var term = "first"; // it could be "FIRST" or fIRST
var re = new RegExp("^" + term, "i") ;
//option 1
//var t = mystring.replace(re,"<span style='font-weight:bold; color:Blue;'>" + term + "</span>");
//option 2
var t = mystring.replace(re,term.bold().fontcolor("Blue"));
上面的代码给出了蓝色的 first LAST,但是我希望它是 FiRst LAST 作为 mystring 的大小写顺序
也许可以使用 indexof() 方法,但可能有一种简单有效的方法。
【问题讨论】:
-
换句话说,您需要将所有字符串大写,但
term匹配的单词保持不变? -
看看这个可能有帮助.... [stackoverflow.com/questions/3294576/… [1]: stackoverflow.com/questions/3294576/…
标签: javascript jquery