javascript
  • regex
  • 2010-09-06 74 views 1 likes 
    1

    我必須將字符串(用戶生成)轉換爲正則表達式規則。Javascript正則表達式 - 每個符號但不是XX

    我的問題是,我必須說,

    更換種種跡象但不 A-B,0-9,減,點和逗號

    我希望有人可以提供幫助。

    HTML

    <div id="d1" class="line1"></div> 
    

    JS

    $(function() { 
    
        new_regex_rule = 'hello,bl.com,dkd-dkd.com,blub,blib,satssan kommt'; 
    
        // new_regex_rule = new_regex_rule.replace(/[a-z][0-9][-.]/gi,''); 
    
        $('#d1').append('<hr />'+new_regex_rule+'<hr />'); 
    
        if(new_regex_rule.match(/\s/)){ new_regex_rule = new_regex_rule.replace(/\s/,'\\s'); } 
        if(new_regex_rule.match(/,/)){ new_regex_rule = new_regex_rule.replace(/\,/,'|'); } 
    
        $('#d1').append('<hr />'+new_regex_rule+'<hr />'); 
    }); 
    

    工作示例

    http://www.jsfiddle.net/V9Euk/517/

    在此先感謝! 彼得

    編輯:或者是IST也許可以使用字符串作爲正則表達式規則,因爲它是什麼?

    回答

    1

    這一個替代每個星座而不是AB,0-9,減,點和逗號(區分大小寫)

    var regex = /[^a-z0-9.,]/g 
    alert("[email protected]?".replace(regex,"X")); 
    
    +2

    無需逃避'.'字符集和內','反正。 – Gumbo 2010-09-06 07:16:07

    +1

    在這裏測試它:http://jsfiddle.net/awhVc/ – 2010-09-06 07:21:16

    +0

    @Gumbo感謝您的教訓。這是我的共同行爲,我現在知道已經過時;)修復它。 – 2010-09-06 07:23:21

    相關問題