sampleText='6501-*-5-[*]'
result='6501-%-5-[*]'
我不得不更換*由%和離開[*]原樣。兩者可能有多重發生。我們如何在JavaScript中做到這一點。提前致謝。
sampleText='6501-*-5-[*]'
result='6501-%-5-[*]'
我不得不更換*由%和離開[*]原樣。兩者可能有多重發生。我們如何在JavaScript中做到這一點。提前致謝。
試試這個:
sampleText = sampleText.replace(/\*/g, function(match, offset){
if(offset > 0) {
if(sampleText[offset - 1] == '[' && sampleText[offset + 1] == ']'){
return match;
}
}
return '%';
});
小提琴:here
您可以使用String.replace()方法。 Doc here。
例如:
text = '6501-*-5-[*]';
text = text.replace('-*-','-%-');
console.log(text); // 6501-%-5-[*]
取代**第一** - * - 與 - % - 不是他的要求 –
[?你嘗試過什麼(http://mattgemmell.com/2008/12/ 08/what-you-you-tried /) – heretolearn