1

Using this initialization statement:

  var str = $('#res').text();

this gives :

Details are below 1. Payables 2. Purchasing 3. Cash Management

I want to put line break <br> before each number so that the page looks like

Details are below 

1. Payables 
2. Purchasing 
3. Cash Management

I tried :

str.replace(/[^d.,]+/,'<br>');

But it is not working as expected.

2
  • 3
    Are you looking for something like this: 'Details are below 1. Payables 2. Purchasing 3. Cash Management'.replace(/\d+\./g, function(m) => { return '<br>' + m; });? Commented Jan 18, 2016 at 5:12
  • I tried with it in console and getting SyntaxError: expected expression, got '=>' Commented Jan 18, 2016 at 5:18

2 Answers 2

2

You could use a regular expression and replacements

$('#res').html(function(i, html) {
    return html.replace(/(\d+)/g, '<br />$1');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="res">Details are below 1. Payables 2. Purchasing 3. Cash Management</div>

Sign up to request clarification or add additional context in comments.

Comments

1
<div id="abc">Details are below 1. Payables 2. Purchasing 3. Cash Management</div>
<div id="cd"></div>
<script>
var ab = $("#abc").text();
var cd = ab.replace(/(\d+)/g,"<br/> $1");
$("#cd").html(cd);

</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.