简体   繁体   English

JavaScript 在给定单词处断句

[英]JavaScript break sentence at a given word

I'm having a situation where my data is coming from server side and I have to break the sentence into two lines,我遇到的情况是我的数据来自服务器端,我必须将句子分成两行,

Example: Payment: xyz is done Removal: Refund made

Now I have to make this sentence into two lines by adding a <br> tag before Removal现在我必须通过在Removal之前添加一个<br>标签将这句话分成两行

Need help, tried with indexOf , split but I am unable to get the workaround.需要帮助,尝试使用indexOfsplit但我无法获得解决方法。

I assume the Removal: part is always present in your string .我假设Removal:部分始终存在于您的string

So what you can do is to :所以你可以做的是:

This is how should be your code:这是您的代码应该如何:

 var str = "Payment: xyz is done Removal: Refund made"; let ind = str.indexOf("Removal"); let sentences = [str.substr(0, ind), str.substr(ind)]; console.log(sentences.join('<br/>'));

You can write a function to dynamically add this text to the div you want the text to be added to.您可以编写一个函数来将此文本动态添加到要添加文本的 div 中。

I have assumed that you are using JavaScript and HTML only.我假设您只使用 JavaScript 和 HTML。

Please find below code.请找到下面的代码。

 function breakString(str){ let breakIndex = str.indexOf("Removal"); let arr = [str.substring(0,breakIndex), str.substring(breakIndex)]; let parent = document.getElementById("myDiv"); parent.innerHTML = arr[0] + "</br>" + arr[1]; } breakString("Payment: xyz is done Removal: Refund made");
 <div id="myDiv"></div>

Just split it after done : done后将其拆分:

var str = "Payment: abc is done Removal: Refund made";
strArr.split("done");

Then add done back in, concatenate it, and add the <br> :然后重新添加done ,连接它,并添加<br>

str = strArr[0] + "done <br>" + strArr[1];

You can use RegEx to capture every that come before Removal and Everything other text that come after include Removal it self.您可以使用 RegEx 来捕获在Removal之前出现的所有文本以及在包括 Removal it self 之后出现的所有其他文本。

Because the structure of your text will not change.因为你的文本结构不会改变。 you will have exactly 3 parts in the matches variable您将在matches变量中恰好有 3 个部分

  1. The string itself Payment: xyz is done Removal: Refund made字符串本身Payment: xyz is done Removal: Refund made
  2. The text before Removal Payment: xyz is done搬家Payment: xyz is done前的文字Payment: xyz is done
  3. And the remaining text Removal: Refund made和剩余的文字Removal: Refund made

 chain = "Payment: xyz is done Removal: Refund made" matches = chain.match(/(.+)(Removal:.+)/); console.log(matches.slice(1).join('<br>'))

You can use .replace() method to do the task, and as it returns a string with the desired replacement(s), your first still there and untouched, so you'd probably use in another task for example.您可以使用.replace()方法来完成任务,当它返回一个带有所需替换的字符串时,您的第一个字符串仍然存在且未受影响,因此您可能会在另一个任务中使用。

Here's a demo:这是一个演示:

 var str = 'Example: Payment: xyz is done Removal: Refund made', newStr = str.replace(/(Removal:)/i, '<br />$1'); document.body.innerHTML = newStr;

Here's a regex one:这是一个正则表达式:

var s = "Payment: xyz is done Removal: Refund made";
var spl = s.split(/^(Payment: [\w]*)\s*(Removal: [\w]*)$/);
console.log(spl[1]+'<br>'+spl[2]);

EDIT:编辑:

with replace is even nicer用替换更好

var s = "Payment: xyz is done Removal: Refund made";
var spl = s.replace(/^(Payment: [\w]*)\s*(Removal: [\w]*)$/,"$1<br>$2");
console.log(spl);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM