2

Here is a text in html

...now have strategies to |lead our company| successfully...

and here is what I want using jquery

...now have strategies to <strong>lead our company</strong> successfully...

I tried

jQuery(function(){ 
   jQuery('.quotes').each(function() { 
     console.log(jQuery(this).text()); 
     var text = jQuery(this).text().replace(/[|]/g,"<strong>"); 
     var texts = jQuery(text).replace(/[|]/g,"</strong>");
     jQuery(this).html(text); 
   }); 

});
4
  • Did you really have | symbol in your string? Commented Mar 16, 2017 at 8:19
  • Yes, I have this symbol in the string Commented Mar 16, 2017 at 8:21
  • stackoverflow.com/help/mcve Commented Mar 16, 2017 at 8:22
  • I did but it makes the bold string up to the end of the string. not the string in between the symbol Commented Mar 16, 2017 at 8:23

1 Answer 1

2

You can use a regex with a global modifier like this:

\|([^|]+)\|

with a replacement of:

'<strong>$1</strong>'

The | is a special character in regex and can be escaped (\|) to refer to it literally.

The ([^|]+) captures a group containing at least one non | character.

var text = '...now have strategies to |lead our company| successfully...';
text = text.replace(/\|([^|]+)\|/g, '<strong>$1</strong>');
console.log(text);

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

1 Comment

This is a better answer than mine as yours will work when there's multiple |...| sections in the text.

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.