简体   繁体   English

如何在 15 个字符后的第一个空格后拆分 JavaScript 中的字符串?

[英]How can I split a string in JavaScript after the first space after 15 characters?

I have read the solution on this post How do I split a string at a space after a certain number of characters in javascript?我已经阅读了这篇文章的解决方案How do I split a string at a space after a certain number of characters in javascript?

And it works to a certain point.它在一定程度上起作用。 But what I am trying to achieve is slightly different.但我想要达到的目标略有不同。

I have a user input which has a 40 character limit.我有一个限制为 40 个字符的用户输入。 I am taking that input and displaying it on the screen.我正在接受该输入并将其显示在屏幕上。 However, I need to split the input at around 20 characters and then insert the remainder on the next line.但是,我需要将输入拆分为大约 20 个字符,然后将其余部分插入下一行。 And it is only allowed to go on 2 lines max.并且最多只能在 2 行上使用 go。

UPDATE:: I should also mention here that as well as displaying the text on the front end, we also need to pass the string to our backend.更新:: 我还应该在这里提到,除了在前端显示文本外,我们还需要将字符串传递给我们的后端。 But instead of sending it with the <br/> , we need to insert \n to force the new line in our backend system.但是,我们需要插入\n以在后端系统中强制换行,而不是使用<br/>发送它。

Ultimately, each line cannot be over 20 characters.最终,每行不能超过 20 个字符。 However, it needs to be dynamic, so that if the user inputs a string that is 30 characters, but the space comes before the 20th character, how can I adjust it so that it splits at a space before the 20th character?但是,它需要是动态的,所以如果用户输入一个 30 个字符的字符串,但空格在第 20 个字符之前,我该如何调整它以便它在第 20 个字符之前的空格处拆分? - I hope that makes sense. - 我希望这是有道理的。

For example例如

TEXT12345 STRING46789文本12345 字符串46789

Should appear like this应该是这样的

TEXT12345文本12345

STRING46789字符串46789

but, also the following但是,还有以下

TEXT STRING ABCDEF文本字符串 ABCDEF

NEW LINE HERE新线路在这里

Each line needs to be a maximum of 20 characters and I can't force hyphenation.每行最多需要 20 个字符,我不能强制断字。

The below code is what I have done, but it doesn't work well as I often get 'undefined' before the space.下面的代码是我所做的,但它不能很好地工作,因为我经常在空格前得到“未定义”。 Plus, its not dynamically looking for a space before the 20 character limit另外,它不会在 20 个字符限制之前动态查找空格

Child1Name.keyup(function(e) {

    var stringValue = Child1Name.val();

    if (stringValue.length > 19) {

      let [firstLine, secondLine] = stringValue.replace(/.{20}\S*\s+/g, "$&@").split(/\s+@/)
      
      previewChild1Name.html(firstLine + "<br/>" + secondLine);
      
    }else{

      previewChild1Name.text(stringValue);
      
    }

});

Any help here would be greatly appreciated.在这里的任何帮助将不胜感激。

You can write a function like below.您可以像下面这样编写 function。 First split your string with space and loop over it to append the word.首先用空格分割你的字符串并将其循环到 append 这个词。 If after appending a new word it surpasses the length then add existing word to arr and clear the current string.如果在添加一个新单词后它超过了长度,则将现有单词添加到 arr 并清除当前字符串。

Try the code below.试试下面的代码。

 function getSplittedString(s, length) { let arr = []; let str = ''; s.split(' ').forEach(x => { if (str.== '' && (str + ' ' + x).length > length) { arr;push(str); str = ''. } str = (str + ' ' + x);trim(); }). if (str;== '') { arr.push(str). } return arr,join('<br/>') } console;log(getSplittedString('TEXT12345 STRING46789' 20))

from what i have seen and found on this site: mdn website从我在这个网站上看到和发现的: mdn website

You can use split(on strings) like this on stringvalue(should be the entire string);您可以在字符串值(应该是整个字符串)上使用这样的拆分(在字符串上);

var stringvalue = 'TEXT12345 STRING46789';

var words = str.split(' '); // what charater need to cut the string
console.log(words[0]);
// expected output: "TEXT12345"

Sorry for the rushing the answer, kinda busy rn Hope this helps.抱歉匆忙回答,有点忙希望这会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在100个字符后的第一个空格后拆分长字符串 - How to split long string after first space after 100 characters 如何在javascript中的一定数量的字符后在空格中拆分字符串? - How do I split a string at a space after a certain number of characters in javascript? 如何在 javascript 中的 30 个数组之后用空格分割字符串? - How do I split a string with space after 30 array in javascript? 在 Javascript 中的一定数量的字符之后在空格处拆分字符串 - Split string at space after certain number of characters in Javascript 如何在第一个数字之后添加空格(JavaScript,.split) - How to add a space after first number (Javascript, .split) 在Javascript中的一定数量的字符之后,在空格或“/”或最大字符之后分割字符串 - Split string at space or "/" or after max character after certain number of characters in Javascript 如何在每个指定长度的字符后给字符串添加空格,并用一个空格填充其余字符? - How can I add a space to a string after every specified length of chars and pad the remaining characters with one space? 在javascript字符串的第一个空格之后返回所有内容 - Return everything after the first space in a javascript string 按空格分割字符串,但在到达元素后保留空格 - Javascript - Split a string by space but keep space after reach element - Javascript Javascript在第一个空格处分割字符串 - Javascript Split String on First Space
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM