2
替換所有匹配我有查找文本並使用jQuery
var removeNotification = " (F)";
listVariable = listVariable.replace(removeNotification, '');
這部分工作,但只找到的第一個「(F)」的字符串中,並將其替換爲「」。還有其他許多我需要改變的東西。
我需要的是找到所有匹配項並將其替換的方法。
替換所有匹配我有查找文本並使用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