分解成几个replace
语句(如以下所列)是这样的我会去了解它(working fiddle)。
的fixBullets
功能将关闭所有的子弹成HTML实体和fixBulletEntities
修复的。我这样做是因为我不知道,如果他们是你的源字符串中只是符号字符或HTML实体正常化子弹。
的fixTimes
功能改变“上午09点18分:“进入”9:18上午。 “(否则,fixPeriods
函数使它看起来像”9. 18am“,我确信你不想要。
关于fixCapitalsEndSentence
函数的一个主要警告......这也将字符串转换为”WORDS“ WO。的RDS”这可能不是你想要的。
至少,这应该让你开始...
function fixBullets(text) {
var bullets = /•/g;
return text.replace(bullets, '•');
}
function fixBulletEntities(text) {
var bulletEntities = /•/ig;
text = text.replace(bulletEntities, '. ');
if (text.indexOf('. ') === 0) {
text = text.substring(2);
}
return text;
}
function fixTimes(text) {
var times = /(\d+)[\.:](\d+[ap]m):?/ig;
return text.replace(times, ' $1:$2. ');
}
function fixPeriods(text) {
var periods = /[.](\w+)/g;
return text.replace(periods, '. $1');
}
function fixCapitalsEndSentence(text) {
var capitalsEndSentence = /([A-Z]{2,})([a-z]+)/g;
text = text.replace(capitalsEndSentence, function(match1, match2, match3) {
var len = match2.length - 1;
var newText = match2.substring(0, len) + '. ' + match2.substring(len, len + 1) + match2.substring(len + 1) + match3;
return newText;
});
return text;
}
function fixMultipleSpaces(text) {
var multipleSpaces = /\s+/g;
return text.replace(multipleSpaces, ' ');
}
function fixAll(text) {
text = fixBullets(text);
text = fixBulletEntities(text);
text = fixTimes(text);
text = fixPeriods(text);
text = fixCapitalsEndSentence(text);
text = fixMultipleSpaces(text);
return text;
}
你downvoted我吗?我不是一个正则表达式亲,所以我一当涉及到这些事情时,它有点无知。或许你可以帮助 – 2012-03-22 11:29:15