2

我想替换所有非字母数字字符,并用下划线替换空格。到目前为止,我已经使用多个正则表达式提出了这个问题,但有没有更“有效”的方法?

"Well Done!".toLowerCase().replace(/\s/, '-').replace(/[^\w-]/gi, '');

做得好

4

2 回答 2

2

至少在其他语言中,调用正则表达式引擎的成本很高。我不确定这是否适用于 JavaScript,但这是“C 风格”的做法。我确信自己对其性能进行基准测试将是一次宝贵的学习经验。

var x = "Well Done!";
var y = "";
var c;
for (var i = 0; i < x.length; i++)
{
    c = x.charCodeAt(i);
    if (c >= 48 && c <= 57 || c >= 97 && c <= 122)
    {
        y += x[i];
    }
    else if (c >= 65 && c <=  90)
    {
        y += String.fromCharCode(c+32);
    }
    else if (c == 32 || c >= 9 && c <= 13)
    {
        y += '-';
    }
}
$('#output').html(y);

有关 ASCII 代码,请参见http://www.asciitable.com/。这是一个 jsFiddle。请注意,我还toLowerCase()简单地通过在大写字母上添加 32 来实现您的。


免责声明

当然,就个人而言,我更喜欢可读strtr的代码,因此更喜欢正则表达式,或者如果 JavaScript 中存在某种函数,则使用某种函数。这个答案纯粹是为了教育。

于 2013-08-20T13:38:31.530 回答
1

Note: I thought I could come up with a faster solution with a single regex, but I couldn't. Below is my failed method (you can learn from failure), and the results of a performance test, and my conclusion.

Efficiency can be measured many ways. If you wanted to reduce the number of functions called, then you could use a single regex and a function to handle the replacement.

([A-Z])|(\s)|([^a-z\d])

REY

The first group will have toLowerCase() applied, the second will be replaced with a - and the third will return nothing. I originally used + quantifier for groups 1 and 3, but given the expected nature of the text, removing it result in faster execution. (thanks acheong87)

'Well Done!'.replace(/([A-Z])|(\s)|([^a-z\d])/g, function (match, $0, $1) {
    if ($0) return String.fromCharCode($0.charCodeAt(0) + 32);
    else if ($1) return '-';
    return '';
});

jsFiddle

Performance

My method was the worst performing:

Acheong87  fastest
Original   16% slower
Mine       53% slower

jsPerf

Conclusion

Your method is the most efficient in terms of code development time, and the performance penalty versus acheong87's method is offset by code maintainability, readability, and complexity reduction. I would use your version unless speed was of the utmost importance.

The more optional matches I added to the regular expression, the greater the performance penalty. I can't think of any advantages to my method except for the function reduction, but that is offset by the if statements and increase in complexity.

于 2013-08-20T16:15:57.717 回答