- 統計字串中內某字元或字串出現次數
/* * JavaScript * 找出cloudchen有幾個c */ var str = "cloudchen" var find = "c"; var reg = new RegExp(find,"g") var result = str.match(reg); var count = (result)?result.length:0; console.log('共:%d次',count);
- 統計字串中內某字元或字串出現次數
去-,:,空白,由於"-"是保留字,所以在前面要加\
//C#.Net System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"[:|\-| ]"); string datetime = "2009-01-02 03:04:05"; datetime = reg.Replace(datetime, ""); Console.WriteLine(datetime); //輸出20090102030405
.Net的Regex位於System.Text.RegularExpressions之下
References:
Regular Expression (RegExp) in JavaScript 還可以直接測試
regular language 說明表格
沒有留言:
張貼留言