这些字符串可能是长段落,所以我不确定最好用空格分隔符分割整个字符串。我试着得到前10个单词并将它们包裹起来:
'<span class="easing">' + string + '</span>'
然后用原始分割的后半部分重新加入。建议以超高效的方式做到这一点?它一次最多会影响页面上的三个段落。
EDITED
这是一个踢球者 - 分裂应该发生在第一个单词结尾的第9个单词之后(如果那个句子少于9个单词)。
示例
var origString = 'Coming into the world on Elvis’ birthday with a doctor named Presley seemed fortuitous until, wielding the silvery smooth scalpel in his aged unsteady hand, the doctor sliced through the walls of my mother’s uterus and into my unborn skin. Inside the warm soothing waters of my mother’s womb, inside the silent weightlessness, I was safe. Then the prick of cold steel marked the first in a series of rude awakenings. I was scarred for life even before birth.';
var newString = '<span="easing">Coming into the world on Elvis’ birthday with a doctor</span> named Presley seemed fortuitous until, wielding the silvery smooth scalpel in his aged unsteady hand, the doctor sliced through the walls of my mother’s uterus and into my unborn skin. Inside the warm soothing waters of my mother’s womb, inside the silent weightlessness, I was safe. Then the prick of cold steel marked the first in a series of rude awakenings. I was scarred for life even before birth.';
或者用一个简短的句子开始该段落:
var origString = '“Is he okay? Tell me everything’s okay” she pleas, her desperate need to confirm my health competing with her own need for consolation.';
var newString = '<span class="easing">“Is he okay?</span> Tell me everything’s okay” she pleas, her desperate need to confirm my health competing with her own need for consolation.';
答案 0 :(得分:2)
考虑到你最多只扫描100个字符(除非你有URI或很长的字)然后逐字符扫描是非常理想的。你可以在某些地方使用.indexOf()来优化这一点,但是你必须检查你必须检查每个可以终止一个句子的不同角色所获得的东西。
function spanomatic ( str, words ) {
var i, l, c;
for ( i=0, l=str.length; i<l; i++ ) {
c = str.charAt(i);
if ( c == ' ' ) {
if ( words-- <= 0 ) {
str = '<span>'+str.substring(0,i)+'</span>'+str.substring(i);
break;
}
}
else if ( ('?!.;:').indexOf(c) != -1 ) {
str = '<span>'+str.substring(0,i)+'</span>'+str.substring(i);
break;
}
}
return str;
}
spanomatic ( 'Pass your string here', 9 );
(上面的代码假设你的文本总是被正确地格式化终止(即至少包含一个?!。; :) - 如果没有,那么一个少于9个单词的段落就有可能结束这可以通过一些变化来解决,但是......)
备注未来读者
如果你想要一个超级高效的&#39;做字符串搜索的方法避免正则表达式(除非你真的需要它们的力量)。这个问题的接受答案很简洁,很好地将功能放在一起 - 不要误解我 - 但它比用for循环扫描字符串要快70%(在我的测试中) FireFox&amp; Chrome至少) ......甚至在将正则表达式定义移到Bergi的函数之外进行比较时(即使用预编译的正则表达式而不是重新创建)每次调用函数时都会出现。
答案 1 :(得分:1)
return string.replace(/.+?[,.?!]|.+$/, function(match, index, string){
var words = match.split(/\s+/);
words[ words.length<10 ? words.length-1 : 9 ] += '</span>';
return '<span class="easing">' + words.join(" ");
});
这匹配第一个类似于句子的东西(或整个字符串 - 除非换行符),并在该跨度中包装它的前10个单词。适用于您的样本输入,也适用于较小的输入。返回空字符串的空字符串,如果需要空跨度,请将正则表达式更改为…|.*$
。
答案 2 :(得分:0)
这段代码怎么样:
var str = 'asda adsfadsf asdfadfadsf adsfsdafadf. adfadfadfad adfadfdaf adfadfadf adfadf \afsgasfggasfg SFGDFGDSFGH dfghdsghdgas hadghdagh';
var sentences = [], words = str.split(' ');
for (var i = 0; i < 9; i++) {
if (words[i].lastIndexOf('.') !== -1) {
sentences.push(words[i]);
break;
} else {
sentences.push(words[i]);
}
}
words.slice(sentences.length, words.length);
$('<span>' + sentences.join(' ') + '</span>').appendTo($('#log'));
我在fiddle下有它,所以你可以测试。您可能希望在循环中使用其余的arr1。
<强>更新强>
如果它不仅仅是完全停止而且还是?!:;等等。然后创建RegExp
并测试,而不是执行lastIndexOf('.')
答案 3 :(得分:0)
下面。虽然它有点代码高尔夫球。遗憾。
$( 'p' ).html(function ( i, text ) {
var re = /(.+?)\s/g, c = 0, res;
while ( res = re.exec( text ) ) if ( ++c === 10 || res[1].slice( -1 ) === '.' ) break;
var p = re.lastIndex;
return '<span class="easing">' + text.slice( 0, p ) + '</span>' + text.slice( p );
});