Remove Character From String Using jQuery

In this tutorial, learn how to remove character from the string on the button click with jQuery. The short answer is to use replace() of jQuery to remove any single character from the string.

You need to specify the letter which you want to remove from the string. You need to use the click event of jQuery to remove the string on button click.

Method 1: jQuery Remove Character From String Using replace() with ‘/g’

To perform this task, you have to use the jQuery text() to get the string characters. Use a variable where you want to get the string characters.

Now, suppose you want to remove the character named ‘s’ from the string. You have to use the jQuery replace(). The first argument of the replace function requires the single string name which you want to remove.

The /g in the replace function scans the mentioned character from all the letters of the string. It means global characters in a string.

Test it Live

Output

This is my string

The above example contains the string and the button. Click the above button to remove the single character ‘s’ from the string. You will get a new string below the button without any character ‘s’.

Method 2: Delete Character From String Using replace() and RegExp()

If you delete characters from a string, you can use the replace() inside which you have to use the RegExp() as the first argument. The second argument is space within quotes (‘ ‘).

Also, specify the first argument of the RegExp() function as the character to delete from the string. The second argument of the RegExp() function is ‘g’ to scan globally all the characters of the string.

Output

This is my string

When you click the above button, you will get the new string without character ‘s’. It is simple to use without the need to use slashes (/) given in the previous example.

Method 3: Remove Single Letter From the String Using split() and join()

In addition to the above example, you can also remove the character from the string using the split() and join() function. You have to pass the character as the argument of the split() function.

Output

This is my string

You have to click the above button to remove the character ‘i’ from the string. This is a useful and simple example to removing all the specified letters from the text.

You may also like to read.

I hope you like this post on how to remove character from string using jQuery.

Reference