查找文本并使用jQuery
问题描述:
替换所有匹配我有查找文本并使用jQuery
var removeNotification = " (F)";
listVariable = listVariable.replace(removeNotification, '');
这部分工作,但只找到的第一个“(F)”的字符串中,并将其替换为“”。还有其他许多我需要改变的东西。
我需要的是找到所有匹配项并将其替换的方法。
答
试试这个:
var removeNotification = /\s\(\F\)/g; // "g" means "global"
listVariable = listVariable.replace(removeNotification, '');
console.log(listVariable)
这将替换所有的比赛,不只是第一个。
答
你可以这样做,如果removeNotification
不能被硬编码:
// escape regular expression special characters
var escaped = removeNotification.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
// remove all matches
listVariable = listVariable.replace(new RegExp(escaped, 'g'), '');
为什么java的?????? – 2013-04-23 09:54:07
错误的标记对不起 – Okky 2013-04-23 09:54:56