javascript - 如何在第 n 个空格处将一个长字符串一分为二?

标签 javascript string

这些字符串可能是很长的段落,所以我不确定最好用空格分隔符分割整个字符串。我正在尝试获取前 10 个单词并将它们包装在一个跨度中:

'<span class="easing">' + string + '</span>'

然后将其与原始拆分的后半部分重新合并。关于执行此操作的 super 有效方法的建议?它一次最多会影响页面上的三个段落。

已编辑

这里有一个关键点——拆分应该出现在第 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.';

最佳答案

考虑到您最多只扫描大约 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 个单词的段落可能会结束无跨度。这可以通过一些更改来解决...)

future 读者注意事项

如果您正在寻求一种“ super 高效”的字符串搜索方式,请避免使用正则表达式(除非您确实需要它们的强大功能)。这个问题的公认答案是简洁且很好地组合在一起的功能 - 不要误会我的意思 - 但它比仅使用 for 循环扫描字符串慢大约 70%(至少在我对 FireFox 和 Chrome 的测试中) ...甚至在将正则表达式定义移到 Bergi 的函数之外进行比较时也是如此(即使用预编译的正则表达式而不是每次调用函数时都重新创建它们)。。 p>

http://jsperf.com/compare-regexp-vs-char-scanning

关于javascript - 如何在第 n 个空格处将一个长字符串一分为二?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12440066/

相关文章:

javascript - Windows 通用应用程序 - Javascript - 文件 IO - 如果文件已存在则崩溃

javascript - AngularJS 过滤器去除某个正则表达式

java - 在线作业的高级字符串操作

python - 为什么 str(reversed(...)) 不给我反转的字符串?

java - 用java从字符串矩阵中替换字符串中的某个单词

java - 使用单独的开始/结束字符连接字符串

javascript - iFraming mht 文件

Javascript:Textarea到html,如何在html中保存精确的空格数?

javascript - RamdaJS : After an object operation my keys are getting re-ordered alphabetically? 是预期的吗?

python - 使用新的 Python 格式函数舍入小数