Converting user input string to regular expression

前端 未结 11 1946
无人及你
无人及你 2020-11-22 14:18

I am designing a regular expression tester in HTML and JavaScript. The user will enter a regex, a string, and choose the function they want to test with (e.g. search, match,

11条回答
  •  遥遥无期
    2020-11-22 14:38

    Thanks to earlier answers, this blocks serves well as a general purpose solution for applying a configurable string into a RegEx .. for filtering text:

    var permittedChars = '^a-z0-9 _,.?!@+<>';
    permittedChars = '[' + permittedChars + ']';
    
    var flags = 'gi';
    var strFilterRegEx = new RegExp(permittedChars, flags);
    
    log.debug ('strFilterRegEx: ' + strFilterRegEx);
    
    strVal = strVal.replace(strFilterRegEx, '');
    // this replaces hard code solt:
    // strVal = strVal.replace(/[^a-z0-9 _,.?!@+]/ig, '');
    

提交回复
热议问题