Re: [問題] javascript replace的正規表達式
※ 引述《BA0 (寶)》之銘言:
: 小弟希望把一個字串內的幾個特定內容換成另外幾個
: 文字表達比較混亂-_-
: 就是象 str1="sfjoabysfjslsfdctjpsg"
: 想把其中的a都換成@,s都換成$
: 我知道可以用
: str1=str1.replace(/a/g,"@");
: str1=str1.replace(/s/g,"$");
: 但是我想問用一次replace完成,應該怎麼寫
: 如果字串很長,跑兩次很低效
: 感謝大大回答
你可以這樣寫
str1=str1.replace(/[as]/g,function(token){
if(token == "s") return "@";
else return "$";
});
token會拿到pattern match到的字
--
I am a person, and I am always thinking .
Thinking in love , Thinking in life ,
Thinking in why , Thinking in worth.
I can't believe any of what ,
I am just thinking then thinking ,
but worst of all , most of mine is thinking not actioning...
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 111.82.145.85
推
08/11 23:18, , 1F
08/11 23:18, 1F
→
08/11 23:18, , 2F
08/11 23:18, 2F
→
08/11 23:19, , 3F
08/11 23:19, 3F
推
08/11 23:30, , 4F
08/11 23:30, 4F
→
08/11 23:30, , 5F
08/11 23:30, 5F
推
08/11 23:38, , 6F
08/11 23:38, 6F
→
08/11 23:38, , 7F
08/11 23:38, 7F
→
08/12 00:44, , 8F
08/12 00:44, 8F
推
08/12 18:20, , 9F
08/12 18:20, 9F
→
08/12 18:20, , 10F
08/12 18:20, 10F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
Ajax 近期熱門文章
PTT數位生活區 即時熱門文章
1
10