通过javascript正则表达式纠正句子结构
下面我有一个通过javascript正则表达式纠正句子结构,javascript,regex,Javascript,Regex,下面我有一个sentance和desiredResult,用于sentance。使用下面的模式,我可以找到需要更改为t,t的t,但我不知道该怎么做 var sentence = "Over the candidate behaves the patent Then the doctor."; var desiredResult = "Over the candidate behaves the patent, then the doctor."; var pattern = /[a-z]\s[A
sentance
和desiredResult
,用于sentance
。使用下面的模式
,我可以找到需要更改为t,t
的t
,但我不知道该怎么做
var sentence = "Over the candidate behaves the patent Then the doctor.";
var desiredResult = "Over the candidate behaves the patent, then the doctor.";
var pattern = /[a-z]\s[A-Z]/g;
如果前面的字母是小写字母,我想在大写字母前添加逗号和空格,而不是“I”,以纠正句子。对句子使用.replace()
,并将替换函数作为第二个参数传递
var corrected = sentence.replace(
/([a-z])\s([A-Z])/g,
function(m,s1,s2){ //arguments: whole match (t T), subgroup1 (t), subgroup2 (T)
return s1+', '+s2.toLowerCase();
}
);
至于保留大写的I
,有很多方法,其中之一是:
var corrected = sentence.replace(
/([a-z])\s([A-Z])(.)/g,
function(m,s1,s2,s3){
return s1+((s2=='I' && /[^a-z]/i.test(s3))?(' '+s2):(', '+s2.toLowerCase()))+s3;
}
);
但是有更多的情况下它会失败,比如:他的名字是乔。
,WTF是多么可怕的失败的缩写。
和其他许多。在句子中使用.replace()
,并将替换函数作为第二个参数传递
var corrected = sentence.replace(
/([a-z])\s([A-Z])/g,
function(m,s1,s2){ //arguments: whole match (t T), subgroup1 (t), subgroup2 (T)
return s1+', '+s2.toLowerCase();
}
);
至于保留大写的I
,有很多方法,其中之一是:
var corrected = sentence.replace(
/([a-z])\s([A-Z])(.)/g,
function(m,s1,s2,s3){
return s1+((s2=='I' && /[^a-z]/i.test(s3))?(' '+s2):(', '+s2.toLowerCase()))+s3;
}
);
但是有更多的情况下,它会失败,比如:
他的名字是乔。
,WTF是多么可怕的失败的缩写。
和其他许多。现在还不清楚应该使用什么样的启发式方法来检测结构;你可以用像这样的东西,否则<代码>^.*(专利\sT)。*不清楚应该使用什么样的启发式方法来检测结构;你可以用像这样的东西,否则<代码>^.*(专利\sT)。*我又增加了一条规定,“I”,我不想让“I”单独存在。有什么想法吗?我又加了一条规定,“我”,我不想让“我”一个人呆着。有什么想法吗?