如何在第n个空格中将长字符串拆分为两个?

编程入门 行业动态 更新时间:2024-10-11 17:26:42
本文介绍了如何在第n个空格中将长字符串拆分为两个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这些字符串可能是长段落,所以我不确定最好用空格分隔符分割整个字符串。我试着得到前10个单词并将它们包裹在一个范围内:

These strings may be long paragraphs, so I'm not sure it's best to split the entire string with a space delimiter. I'm trying to get, say, the first 10 words and wrap them in a span:

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

然后使用原始拆分的后半部分重新加入。建议以超高效的方式做到这一点?它一次最多会影响页面上的三个段落。

Then rejoin that with the second half of the original split. Suggestions on a super efficient way to do this? It would affect at most three paragraphs on the page at a time.

已编辑

这是一个踢球者 - 分裂应该发生在第9个单词之后或者在第一个句子结尾处(如果那个句子小于9个单词)。

Here's a kicker — The split should occur after the 9th word OR at the end of the first sentence (if that sentence is less than 9 words).

示例

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.';

推荐答案

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个单词。适用于您的样本输入,也适用于较小的输入。返回空字符串的空字符串,如果想要空跨度,请将正则表达式更改为 ... |。* $ 。

This matches the first sentence-like thing (or the whole string - unless linebreaks), and wraps the first 10 words of it in that span. Works for both your sample inputs, but also on smaller ones. Returns the empty string for an empty string, change the regex to …|.*$ if you want an empty span.

更多推荐

如何在第n个空格中将长字符串拆分为两个?

本文发布于:2023-10-08 01:08:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471063.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:空格   字符串   中将   两个   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!