我有这句话:
It seems to be the basic assumption of traditional philosophies that human <span style="color:#ff5e43;">intellectual</span> powers are for (abc) the purpose of accelerating our own evolution beyond the restraints of the biological determinism which binds all other living organisms.
我需要按照长度匹配单词。我使用这个正则表达式语法,其长度增加了一个循环:
(?:^| )(\S{index})(?= |$)
但是在5时,它也匹配 _&lt; span
在5处,结果应该是:seems basic human (abc) which binds other
在此链接中,我尝试Regex101
答案 0 :(得分:0)
只需使用\w
代替\S
。见here
\w
表示由[A-Za-z0-9_]
组成的单词。
答案 1 :(得分:0)
这里最简单也可能是最好的解决方案是根本不使用正则表达式。相反,使用DOM的强大功能来摆脱不必要的东西,并只查看干净的文本。
// Setup
var string = 'It seems to be the basic assumption of traditional philosophies that human <span style="color:#ff5e43;">intellectual</span> powers are for (abc) the purpose of accelerating our own evolution beyond the restraints of the biological determinism which binds all other living organisms.',
text,
pass = [];
// Define length
var wordlength = 5;
// Make a new div for the elements to sit in
$textNode = $(document.createElement('div')).html(string);
// Remove html tags but keep their content (e.g. the span tag)
text = $textNode.text();
// Split the text at the spaces
text = text.split(' ');
// Loop through the new array to find words with specific length
$.each(text,function(i,el){
if (el.length === wordlength) pass.push(el);
});
var string = 'It seems to be the basic assumption of traditional philosophies that human <span style="color:#ff5e43;">intellectual</span> powers are for (abc) the purpose of accelerating our own evolution beyond the restraints of the biological determinism which binds all other living organisms.',
text,
pass = [],
wordlength = 5;
$textNode = $(document.createElement('div')).html(string);
text = $textNode.text().split(' ');
$.each(text,function(i,el){
if (el.length === wordlength) pass.push(el);
});
document.write(pass.join(' '));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>