开发者

How do I restrict a string to a specific set of characters in JavaScript?

To put this in context, consider these 2 functions:

ml_RestrictToChars = function(input,regex) {
    var result = '';var c = '';
    var rx = new RegExp(regex);
    for (var i = 0; i < input.length; i++) {
        c = input.charAt(i);
        if (rx.test(c)) {
            result += c;
        }
    }
    return result;
};
ml_OmitChars = function(input,regex) {
    var rx = new RegExp(regex,'g');
    return input.replace(rx,''); 
};

The first function will constrain the input to a ce开发者_开发知识库rtain set of characters, the second will omit any character or sequence of characters from the input. As you can see by the code in the first function, it will only function if a single character class is passed into the 'regex' argument because the inclusion code only checks the characters one at a time.

Consider the input string 12-34, right now if I pass a regular expression of '[0-9]' to the first function and '[^0-9]' to the second function, I will get an identical output as expected: 1234

However, if I use a more advanced expression in the second function such as '[^0-9][^0-9]-', I will get an ouput of 12-. But if I pass more than one character class to the first function it will return an empty string.

What I am wondering is if there is an easy way (preferably without looping) to do the inverse of ml_OmitChars so they work similarly?


Matching every character is simple (but slow), and you show how it works. What you want now is matching a pattern and concatenating all matches. This is done like this:

ml_RestrictToChars = function(input,regex) {
    var rx = new RegExp (regex, 'g');
    var matches = input.match (rx);
    return matches.join ('');
};

The first line makes it a "global" regex, which changes the behavior of match(). The second line returns all matches in an array and the last line joins them into a single string and returns it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜