正则表达式错误
Regular Expression Error
下面是我正在使用的正则表达式的最新版本,它抛出错误"无效的正则表达式"。
任何带有正则表达式格式的 foo 将不胜感激!
下面是我的代码:
// This function gets all the text in browser
function getText() {
return document.body.innerText;
}
var allText = getText(); // stores into browser text into variable
//regex set to rid text of all punctuaction, symbols, numbers, and excess spaces
var matcher = new RegExp ("/(?<!'w)[a-zA-Z]+(?!'w)/", "g");
//cleanses text in browser of punctuation, symbols, numbers, and excess spaces
var newWords = allText.match(matcher);
//using a single space as the dividing tool, creates a list of all words
var Words=newWords.split(" ");
而不是
//regex set to rid text of all punctuaction, symbols, numbers, and excess spaces
var matcher = new RegExp ("/(?<!'w)[a-zA-Z]+(?!'w)/", "g");
//cleanses text in browser of punctuation, symbols, numbers, and excess spaces
var newWords = allText.match(matcher);
//using a single space as the dividing tool, creates a list of all words
var Words=newWords.split(" ");
只需使用
var Words = allText.match(/'b[a-zA-Z]+'b/g); // OR...
// var Words = allText.match(/'b[A-Z]+'b/ig);
这将为您提供所有仅由 ASCII 字母组成的"单词",因为String#match
与基于 /g
的正则表达式一起获取与正则表达式匹配的所有子字符串(匹配单词边界之间的 1 个或多个 ASCII 字母)。
JS不支持回溯(即 (?<!)
或(?<=)
结构),您需要在此处'b
单词边界。
请注意,您需要类似 .replace(/'W+/g, ' ')
的东西来删除文本中的所有标点符号、符号、数字和多余的空格,但似乎您可以依靠 .match(/'b[a-zA-Z]'b/g)
.
相关文章:
- 正则表达式给出错误的输出
- 正则表达式中的错误
- javascript使用正则表达式得到错误的结果
- 在正则表达式中添加 (?i) 会导致 javascript 中出现错误“无效的正则表达式组”
- Javascript错误正则表达式不是函数
- Javascript 正则表达式不会在应该给出错误消息时给出错误消息
- 即使验证正则表达式错误,也提交表单
- 用于匹配错误和关联几行的正则表达式
- 什么'用这个正则表达式查找URL是错误的
- 正则表达式仅在Javascript中存在语法错误
- javascript中正则表达式(match)中变量的使用错误
- 服务器错误消息的正确正则表达式
- 未终止的正则表达式文字 js 错误
- 为什么这个 JavaScript 正则表达式会给我一个语法错误
- javascript 错误:未定义 jQuery 和无效的正则表达式:缺少 /
- jQuery 正则表达式测试在 .each 错误中
- 正则表达式语法错误 - 无需重复
- 仅正则表达式 Nan 错误号
- javascript语法错误:正则表达式无效
- 带有浏览器.js的 Babel,获取未捕获的语法错误:正则表达式无效