给定一个自由文本
Hello World http://example.com/?param=FooBar Foo Bar
...除了最常见的 URL 模式(因为它们是需要保留大小写的部分)之外,如何将所有内容都转换为小写字母?
例如以上将最终成为
hello world http://example.com/?param=FooBar foo bar
我正在使用 JavaScript。谢谢!
最佳答案
我会做这样的事情:
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();
})
根据您想要的具体程度,您可以在回调中包含更复杂的 URL 检查 - 如下所示:https://stackoverflow.com/a/3809435/1200182
关于javascript - 正则表达式将所有内容转换为小写,URL 除外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24143337/