正则表达式将所有内容转换为小写,URL除外

时间:2014-06-10 14:15:19

标签: javascript regex

给出像

这样的自由文本
  

Hello World http://example.com/?param=FooBar Foo Bar

...除了大多数常见的网址模式(因为它们是需要保留案例的部分),人们如何才能将所有内容转换为小写字母?

E.g。以上将最终成为

  

hello world http://example.com/?param=FooBar foo bar

我正在使用JavaScript。谢谢!

3 个答案:

答案 0 :(得分:2)

我会做这样的事情:

var str = 'Hello World http://example.com/?param=FooBar Foo Bar';

str.replace(/[^\s]+/g, function (match) {
    return match.indexOf('http') === 0 ? match : match.toLowerCase();
})

http://jsfiddle.net/xMPFW/1/

根据您希望的具体情况,您可以在回调中包含更复杂的网址检查 - 如下所示:https://stackoverflow.com/a/3809435/1200182

答案 1 :(得分:1)

这应该有效:

var s = 'Hello World http://example.com/?param=FooBar FOO Bar';
var r = s.replace(/([^ A-Z]*)([A-Z])/g, function($1, $2) { 
     return /https?:\/\/\S+/.test($1) ? $1 : $1.toLowerCase(); 
});
//=> hello world http://example.com/?param=Foobar foo bar

答案 2 :(得分:1)

对字符串进行标记,然后利用URI Regex from here来检查令牌是否是URI令牌,如果它不是小写的话。

var regex = new RegExp("([A-Za-z][A-Za-z0-9+\\-.]*):(?:(//)(?:((?:[A-Za-z0-9\\-._~!$&'()*+,;=:]|%[0-9A-Fa-f]{2})*)@)?((?:\\[(?:(?:(?:(?:[0-9A-Fa-f]{1,4}:){6}|::(?:[0-9A-Fa-f]{1,4}:){5}|(?:[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){4}|(?:(?:[0-9A-Fa-f]{1,4}:){0,1}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){3}|(?:(?:[0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){2}|(?:(?:[0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}:|(?:(?:[0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})?::)(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))|(?:(?:[0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}|(?:(?:[0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})?::)|[Vv][0-9A-Fa-f]+\\.[A-Za-z0-9\\-._~!$&'()*+,;=:]+)\\]|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|(?:[A-Za-z0-9\\-._~!$&'()*+,;=]|%[0-9A-Fa-f]{2})*))(?::([0-9]*))?((?:/(?:[A-Za-z0-9\\-._~!$&'()*+,;=:@]|%[0-9A-Fa-f]{2})*)*)|/((?:(?:[A-Za-z0-9\\-._~!$&'()*+,;=:@]|%[0-9A-Fa-f]{2})+(?:/(?:[A-Za-z0-9\\-._~!$&'()*+,;=:@]|%[0-9A-Fa-f]{2})*)*)?)|((?:[A-Za-z0-9\\-._~!$&'()*+,;=:@]|%[0-9A-Fa-f]{2})+(?:/(?:[A-Za-z0-9\\-._~!$&'()*+,;=:@]|%[0-9A-Fa-f]{2})*)*)|)(?:\\?((?:[A-Za-z0-9\\-._~!$&'()*+,;=:@/?]|%[0-9A-Fa-f]{2})*))?(?:\\#((?:[A-Za-z0-9\\-._~!$&'()*+,;=:@/?]|%[0-9A-Fa-f]{2})*))?")

var text = 'Hello World http://example.com/?param=FooBar Foo Bar';
var tokens = text.split(' ');
var lowerText = '';

for (var i = 0; i < tokens.length; i++) {
    if (i != 0)
        lowerText += ' ';
    if (!tokens[i].match(regex))
        lowerText += tokens[i].toLowerCase();
    else
        lowerText += tokens[i];
}