Skip to content
Advertisement

Replace a specific character from a string with HTML tags

Having a text input, if there is a specific character it must convert it to a tag. For example, the special character is *, the text between 2 special characters must appear in italic.

For example:

JavaScript

must be converted to:

JavaScript

So I’ve tried like:

JavaScript

it is replacing the star character with <i> but doesn’t work if there are more special characters.

Any ideas?

Advertisement

Answer

You can simply create wrapper and thereafter use regular expression to detect if there is any word that is surrounded by * and simply replace it with any tag, in your example is <i> tag so just see the following

Example

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement