Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用正则表达式将任何字符串分隔为整词、标点符号和;HTML标记_Javascript_Html_Regex - Fatal编程技术网

Javascript 使用正则表达式将任何字符串分隔为整词、标点符号和;HTML标记

Javascript 使用正则表达式将任何字符串分隔为整词、标点符号和;HTML标记,javascript,html,regex,Javascript,Html,Regex,目前我所发现的有效方法是使用空格来匹配。我想能够匹配任意HTML标记和标点符号 var text = "<div>The Quick brown fox ran through it's forest darkly!</div>" //this one uses spaces only but will match "darkly!</div>" as 1 element console.log(text.match(/\S+/g)); //outputs

目前我所发现的有效方法是使用空格来匹配。我想能够匹配任意HTML标记和标点符号

var text = "<div>The Quick brown fox ran through it's forest darkly!</div>"

//this one uses spaces only but will match "darkly!</div>" as 1 element
console.log(text.match(/\S+/g));

//outputs: ["<div>The", "Quick", "brown", "fox", "ran", "through", "it's", "forest", "darkly!</div>"]
var text=“那只敏捷的棕色狐狸在黑暗中穿过森林!”
//这一个只使用空格,但将匹配“darkly!”作为1个元素
console.log(text.match(/\S+/g));
//输出:[“The”,“Quick”,“brown”,“fox”,“run”,“through”,“it's”,“forest”,“darkly!”]
我想要一个匹配的表达式,该表达式将输出:

["<div>", "The", "Quick", "brown", "fox", "ran", "through", "it's", "forest", "darkly", "!", "</div>"]
[,“The”,“Quick”,“brown”,“fox”,“Run”,“through”,“it's”,“forest”,“darkly”,“darkly”,“Quick”,“brown”,“fox”,“Run”,“through”,“it's”,“forest”,“darkly”,“darkly”,“!”,“]
这是一把小提琴:

最后,我将把所有匹配项存储在一个数组中,进行一些处理(在每个单词周围添加一些带有条件数据属性的span标记),然后以修改后的形式重新输出原始字符串。我提到这一点是因为,如果解决方案不让字符串或多或少保持完整,那么它将无法工作


我在网上找到了很多未遂事件解决方案,但是我的正则表达式不够好,无法充分利用它们的工作。

您可以在HTML标记前后添加一个空格,如下所示:

var text = "<div>The Quick brown fox ran through it's forest darkly!</div>"
text = text.replace(/\<(.*?)\>/g, ' <$1> ');
console.log(text.match(/\w+|\S+/g)); // ## Credit to George Lee ##
var text=“那只敏捷的棕色狐狸在黑暗中穿过森林!”
text=text.replace(/\/g',);
console.log(text.match(/\w+\S+/g));/\归功于乔治·李##
那么:

/(<\/?)?[\w']+>?|[!\.,;\?]/g

/(我的建议是:

console.log(text.match(/(<.+?>|[^\s<>]+)/g));
console.log(text.match(/(|[^\s]+)/g));
其中,在正则表达式中:
(|[^\s]+)
指定两个要捕获的字符串

<.+?> returns all <text> strings
[^\s<>]+ returns all strings that don't contain space,<,>
返回所有字符串
[^\s]+返回所有不包含空格的字符串,

在secound one中,您可以添加想要忽略的字符

这条路径充满了危险。从长远来看,您最好使用专用的HTML解析器。是否有理由将表达式与HTML匹配?您能否获得nodeValue或textContent属性并再次匹配?我听说最难的部分是我们要做的是把
分为
,但不是
它是
它是
它是
s
。我很肯定@帕尔帕蒂姆是对的…我真的不确定是否有一个合理的正则表达式解决方案。更不用说其他的例外了…那有什么例外呢包含“日常”或“12:00 AM”…我猜您也希望这些内容保持分组?@talemyn也许,但是OP说“我希望
X
变成
Y
”上面说的代码就是这样。你可以花几个小时猜测他可能需要什么,可能不需要什么。至于我,我会很高兴地继续我的生活,直到他说“哦……那
Z
呢?”感谢您的帮助和解释-感谢。这很有效,只是它包括了与它相邻的单词的标点符号。因此,您得到:[,“the”,“Quick”,“brown”,“fox”,“run”,“through”,“it's”,“forest”,“darkly!”,“],而不是:[,“the”,“the”,“Quick”,“brown”,“fox”,“run”,“through”,“it's”,“forest”,“darkly”!”, ""]