javascript - Use jquery to split a string to new line break at certain character - Stack Overflow

Currently, the string isPackage A (123 queries) Package B (212 queries)Is it possible to use jquery to

Currently, the string is

Package A (123 queries) Package B (212 queries)

Is it possible to use jquery to split it into new line like

  • Package A (123 queries)
  • Package B (212 queries)

I was thinking to insert line break after each closing bracket. But not sure if it is appropriate.


Edit 1

In HTML

<ul class="list">
    <li><span id="requested-package">@Model[0].regDescription[a]</span></li>
    <li>Testing</li>
</ul>

Output - I want to make the Plan A in next line


Edit 2

    var str = $('.hidden-package').text();
    var line = str.replace(')', ') <br>');
    $('.requested-package').html(line);

Current Output

I created a hidden field hidden-packageand from there I managed to get the text value using .text(). The problem now is it only replaces the first occurrence of closing bracket. How can I replace all the closing bracket with <br>. Thanks.

Solution

Managed to solve this by changing the code above to the below.

    var str = $('.hidden-package').text();
    var line = str.replace(/\)/g, ')<br>');
    $('.requested-package').html(line);

Currently, the string is

Package A (123 queries) Package B (212 queries)

Is it possible to use jquery to split it into new line like

  • Package A (123 queries)
  • Package B (212 queries)

I was thinking to insert line break after each closing bracket. But not sure if it is appropriate.


Edit 1

In HTML

<ul class="list">
    <li><span id="requested-package">@Model[0].regDescription[a]</span></li>
    <li>Testing</li>
</ul>

Output - I want to make the Plan A in next line


Edit 2

    var str = $('.hidden-package').text();
    var line = str.replace(')', ') <br>');
    $('.requested-package').html(line);

Current Output

I created a hidden field hidden-packageand from there I managed to get the text value using .text(). The problem now is it only replaces the first occurrence of closing bracket. How can I replace all the closing bracket with <br>. Thanks.

Solution

Managed to solve this by changing the code above to the below.

    var str = $('.hidden-package').text();
    var line = str.replace(/\)/g, ')<br>');
    $('.requested-package').html(line);
Share Improve this question edited Nov 22, 2016 at 1:55 jenna_3108 asked Nov 18, 2016 at 10:45 jenna_3108jenna_3108 4351 gold badge7 silver badges20 bronze badges 1
  • 2 If it's just a "string", then no - use javascript, not jquery - jquery is for DOM manipulation. – fdomn-m Commented Nov 18, 2016 at 10:47
Add a ment  | 

4 Answers 4

Reset to default 3

Use .replace but try to be as specific as possible. Here we look for where you have a closing bracket followed by a space followed by 'Package' followed by another space ') Package '. This should help prevent erroneous results

'Package A (123 queries) Package B (212 queries)'.replace(') Package ', ')\nPackage')
'Package A (123 queries) Package B (212 queries)'.replace(') ', ')\n')

Add any special character where you want to split the string and replace it with a line break, this should add a new line in html.

For example

var string = "Package A (123 queries)~Package B (212 queries)~Package C (212 queries)";
string = string .replace(/~/g, "<br />");

I needed to convert contents of the variable someStr to string using toString() function:

So if my string looks like this:

someStr = 'Package A (123 queries)~ Package B (212 queries)' with the special character "~" inserted (as suggested by @user2703788), convert the variable someStr as:

someStr = someStr.toString(); And then replace

So we may use:

someStr = someStr.toString().replace(/~/g, "<br />");

And display the contents:

$('#myMessage').html(someStr);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745246945a4618452.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信