JavaScript正则表达式.
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JavaScript正则表达式.,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2425字,纯文字阅读大概需要4分钟。
内容图文
![JavaScript正则表达式.](/upload/InfoBanner/zyjiaocheng/823/2c76672546644c9cb6e0036af411bf70.jpg)
我有一个函数可以纠正大写异常大写单词列表:
var line = "some long string of text";
["AppleScript", "Bluetooth", "DivX", "FireWire", "GarageBand",
"iPhone", "iTunes", "iWeb", "iWork", "JavaScript", "jQuery", "MacBook",
"MySQL", "PowerBook", "PowerPoint", "QuickTime", "TextEdit", "TextMate",
// ...
"Wi-Fi", "Xcode", "Xserve", "XMLHttpRequest"].forEach(function(name) {
line = line.replace(RegExp(name, "gi"), name);
});
现在我遇到的问题是大多数输入字符串平均包含0到3个这些单词.显然现在我正在做几十个(可能有几百个;那个数组有一种不可思议的趋势,随着时间的推移而增长)函数调用基本上什么都不做.
如何更快地制作这段代码并摆脱不必要的函数调用?
输入示例:
My iphone application has a user form under UIViewController. When I start application again some of my UIView changes its positions and sizes. (These UIViews depend on keyboard position) Somewhere is definitely my fault. I try to figure what is going on when application starts again from background and where the UIView changes can be done.
解决方法:
您可以构建包含所有单词的正则表达式,通过将每个单词括在括号中来捕获每个单词.在替换中使用它将提供足够的信息来恢复替换函数中的原始单词.
function correct (text, words) {
return text.replace (RegExp ('\\b(?:(' + words.join (')|(') + '))\\b', 'ig'), function (m) {
for (var a = arguments.length - 2; a--;)
if (arguments[a])
return words[a-1] || m;
});
}
console.log (correct ("My iphone itunes divx firewire application has a user form under uiviewcontroller. When I start application again some of my uiview changes its positions and sizes. (These uiviews depend on keyboard position) Somewhere is definitely my fault. I try to figure what is going on when application starts again from background and where the uiview changes can be done.",
["AppleScript", "Bluetooth", "DivX", "FireWire", "GarageBand",
"iPhone", "iTunes", "iWeb", "iWork", "JavaScript", "jQuery", "MacBook",
"MySQL", "PowerBook", "PowerPoint", "QuickTime", "TextEdit", "TextMate",
// ...
"UIViewController","UIView",
"Wi-Fi", "Xcode", "Xserve", "XMLHttpRequest"]));
My iPhone iTunes DivX FireWire application has a user form under UIViewController. When I start application again some of my UIView changes its positions and sizes. (These UIViews depend on keyboard position) Somewhere is definitely my fault. I try to figure what is going on when application starts again from background and where the UIView changes can be done.
This turns out to be faster then the original code.
内容总结
以上是互联网集市为您收集整理的JavaScript正则表达式.全部内容,希望文章能够帮你解决JavaScript正则表达式.所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。