Javascript 计算以特定单词开头的行中的单词数
我想计算包含特定ID(例如*AUY)的特定行中的字数。到目前为止,我已经尝试使用下面的正则表达式来查找该行,但是它不考虑在开始< /P>中的“*”。Javascript 计算以特定单词开头的行中的单词数,javascript,regex,Javascript,Regex,我想计算包含特定ID(例如*AUY)的特定行中的字数。到目前为止,我已经尝试使用下面的正则表达式来查找该行,但是它不考虑在开始< /P>中的“*”。 ^ *(.*\b(?:\\*AUY)\b.*) *$ 我有下面的测试字符串 *AUY: today is holiday so Peter and Mary do not need to go to work . %mor: n|today cop|be&3s n|holiday conj|so n:prop|Peter conj
^ *(.*\b(?:\\*AUY)\b.*) *$
我有下面的测试字符串
*AUY: today is holiday so Peter and Mary do not need to go to work .
%mor: n|today cop|be&3s n|holiday conj|so n:prop|Peter conj|and n:prop|Mary v|do neg|not v|need inf|to v|go prep|to n|work .
%snd: <00:00:00><00:07:37>
%AUY: ok_pfp (0.40) er today is holiday errfr ::: so er Peter and Mary {is} ~ er do not need errfr ::: to go to work . errfr :;:a |
AUY:今天是假日,所以Peter和Mary不需要上班。
%mor:n |今天cop | be&3s n |假日联合| so n:prop |彼得联合|和n:prop |玛丽v |不需要inf |去准备|去工作。
%snd:
%奥伊:好的,今天是假日,彼得和玛丽不需要上班。错误:;:a|
结果应仅为第一个字符串,但返回结果匹配中的第一个和最后一个字符串。
看看这个,试试看:
/^.*?\*AUY:(.*?)$/gmi
function countWord(){
const regex = /^.*?\*AUY:(.*?)$/gmi;
const str = `*AUY: today is holiday so Peter and Mary do not need to go to work .
%mor: n|today cop|be&3s n|holiday conj|so n:prop|Peter conj|and n:prop|Mary v|do neg|not v|need inf|to v|go prep|to n|work .
%snd: <00:00:00><00:07:37>
%AUY: ok_pfp (0.40) er today is holiday errfr ::: so er Peter and Mary {is} ~ er do not need errfr ::: to go to work . errfr :;:a |`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
alert(m[1].match(/\b(\w+)\b/g).length);
}
}
函数countWord(){
常量正则表达式=/^.*?\*AUY:(.*?$/gmi;
康斯特:今天是假日,所以彼得和玛丽不需要上班。
%mor:n |今天cop | be&3s n |假日联合| so n:prop |彼得联合|和n:prop |玛丽v |不需要inf |去准备|去工作。
%snd:
%奥伊:好的,今天是假日,所以彼得和玛丽不需要上班;
让m;
while((m=regex.exec(str))!==null){
//这是避免具有零宽度匹配的无限循环所必需的
if(m.index==regex.lastIndex){
regex.lastIndex++;
}
警报(m[1]。匹配(/\b(\w+)\b/g)。长度);
}
}
试试看:
/^.*?\*AUY:(.*?)$/gmi
function countWord(){
const regex = /^.*?\*AUY:(.*?)$/gmi;
const str = `*AUY: today is holiday so Peter and Mary do not need to go to work .
%mor: n|today cop|be&3s n|holiday conj|so n:prop|Peter conj|and n:prop|Mary v|do neg|not v|need inf|to v|go prep|to n|work .
%snd: <00:00:00><00:07:37>
%AUY: ok_pfp (0.40) er today is holiday errfr ::: so er Peter and Mary {is} ~ er do not need errfr ::: to go to work . errfr :;:a |`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
alert(m[1].match(/\b(\w+)\b/g).length);
}
}
函数countWord(){
常量正则表达式=/^.*?\*AUY:(.*?$/gmi;
康斯特:今天是假日,所以彼得和玛丽不需要上班。
%mor:n |今天cop | be&3s n |假日联合| so n:prop |彼得联合|和n:prop |玛丽v |不需要inf |去准备|去工作。
%snd:
%奥伊:好的,今天是假日,所以彼得和玛丽不需要上班;
让m;
while((m=regex.exec(str))!==null){
//这是避免具有零宽度匹配的无限循环所必需的
if(m.index==regex.lastIndex){
regex.lastIndex++;
}
警报(m[1]。匹配(/\b(\w+)\b/g)。长度);
}
}
使用以下正则表达式
(^.*\*AUY.*$)
您可以使用以下正则表达式
(^.*\*AUY.*$)
你可以让
x
成为你的字符串。然后
(x.match(/(^|\n)\*AUY[^\r\n]*/g) || [])
.map(
function(s) { return s.match(/\S+/g).length; }
);
将返回以字符串“*AUY”开头的相应行中的类词构造数的数组
说明:
正则表达式在字符串的开头或任何换行符之后直接查找字符串*AUY(即,即使该行不在字符串的开头,也在行的开头),以及*AUY的第一个标记之后的任何非CRLF字符(即该行的其余部分)
如果匹配值为null
,则执行匹配后的习惯用法|【】
将返回空数组,从而防止在需要数组而不是空值时出错
最后一步.map
对匹配数组的每个元素进行操作,对非空白匹配项进行计数,并将这些计数作为新数组返回。请注意,我们不需要使用| |[]
习惯用法来保护此匹配,因为不可能进行空匹配,因为该行至少包含非空白字符串*AUY
您可以使用此代码作为起点来完成您真正想要做的事情。祝你好运 让
x
成为你的字符串。然后
(x.match(/(^|\n)\*AUY[^\r\n]*/g) || [])
.map(
function(s) { return s.match(/\S+/g).length; }
);
将返回以字符串“*AUY”开头的相应行中的类词构造数的数组
说明:
正则表达式在字符串的开头或任何换行符之后直接查找字符串*AUY(即,即使该行不在字符串的开头,也在行的开头),以及*AUY的第一个标记之后的任何非CRLF字符(即该行的其余部分)
如果匹配值为null
,则执行匹配后的习惯用法|【】
将返回空数组,从而防止在需要数组而不是空值时出错
最后一步.map
对匹配数组的每个元素进行操作,对非空白匹配项进行计数,并将这些计数作为新数组返回。请注意,我们不需要使用| |[]
习惯用法来保护此匹配,因为不可能进行空匹配,因为该行至少包含非空白字符串*AUY
您可以使用此代码作为起点来完成您真正想要做的事情。祝你好运 你能把你的问题说得更清楚些吗?您试图从提供的测试字符串中提取什么?您只需执行
stri即可