I have this sample string where I would like to replace the star with an opening and closing strong tag using regular expressions in JavaScript:
To increase search results, use the 8** prefix.
877 and 866 will result in more matches than 800 and 888 prefixes.
*Note*: The pattern for a custom number can be more than 7 digits. For example: 1-800-Mat-tres(s)
The ideal output would be:
To increase search results, use the 8** prefix.
877 and 866 will result in more matches than 800 and 888 prefixes.
<strong>Note</strong>: The pattern for a custom number can be more than 7 digits. For example: 1-800-Mat-tres(s)
The only caveat being that if there are two starts in a row (like 8**), that they not be replaced with the strong tags.
Thank you in advance for any assistance.
Maybe you could try something like this?
The
+means 1 or more, so will only match if there is something between the*.The
[^\*]means anything that’s not a star*.UPDATE
I’ve updated the regex above to specify that it doesn’t match nonwhite space character’s in between the
*and and the first and last characters of each match. This prevent’s the highlighted bit below from incorrectly matching:8*
* prefix.Note*877 and 866 will result in more matches than 800 and 888 prefixes. *
Here is the same regex with comments (in javascript)
Finally, here is an example of the javascript you could use:
Just replace the
yourStringDatawith a variable containing the data you want to run the replace against.