0

I have a small text field and if there is more than one whitespace I need to format the string and add a br tag in that second whitespace. If there isn;t then I do not need to do anything. I do not need to target the first or third (if there is one) and there will probably not be any after 3 as this is a short title field. the length of characters will be different and there is not consistent marker, like a comma of period or something, that I can target.

I was unable to find an answer that addressed this. I did find answers using regex but those all had markers like comma to target, I cannot find on that specifically will only target the second occurrence if there is one so any help would be great appreciated.

Code I have now, which targets only the first occurrence.

Array.prototype.forEach.call(awardscount, function() {
  var string = $('#award-' + int).attr('data-award-title');

  var refomrattedTitle = string.replace(" ", "<br>");

  int++;
  console.log(refomrattedTitle);
});

1 Answer 1

3

var test = "foo bar string test".replace(/([^\s]*\s[^\s]*)\s/, "$1<br/>");
console.log(test); // logs "foo bar<br/>string test"

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

1 Comment

perfect, I'll accept the answer as soon as it lets me.

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.