Remove specific character in string javascript

I am creating a form to lookup the details of a support request in our call logging system.

Call references are assigned a number like F0123456 which is what the user would enter, but the record in the database would be 123456. I have the following code for collecting the data from the form before submitting it with jQuery ajax.

How would I strip out the leading F0 from the string if it exists?

$('#submit').click(function () {        
            
var rnum = $('input[name=rnum]');
var uname = $('input[name=uname]');

var url = 'rnum=' + rnum.val() + '&uname=' + uname.val();

Remove specific character in string javascript

Penny Liu

12.8k5 gold badges69 silver badges86 bronze badges

asked May 1, 2012 at 9:51

1

Simply replace it with nothing:

var string = 'F0123456'; // just an example
string.replace(/^F0+/i, ''); '123456'

answered May 1, 2012 at 9:54

Mathias BynensMathias Bynens

140k52 gold badges213 silver badges244 bronze badges

3

Honestly I think this probably the most concise and least confusing, but maybe that is just me:

str = "F0123456";
str.replace("f0", "");

Dont even go the regular expression route and simply do a straight replace.

answered Jun 19, 2019 at 1:46

Remove specific character in string javascript

If you want to remove F0 from the whole string then the replaceAll() method works for you.

const str = 'F0123F0456F0'.replaceAll('F0', '');
console.log(str);

answered Sep 23, 2020 at 8:53

Remove specific character in string javascript

Penny LiuPenny Liu

12.8k5 gold badges69 silver badges86 bronze badges

Another way to do it:

rnum = rnum.split("F0").pop()

It splits the string into two: ["", "123456"], then selects the last element.

Remove specific character in string javascript

Penny Liu

12.8k5 gold badges69 silver badges86 bronze badges

answered May 1, 2012 at 9:54

paulslater19paulslater19

5,7191 gold badge26 silver badges25 bronze badges

0

Regexp solution:

ref = ref.replace(/^F0/, "");

plain solution:

if (ref.substr(0, 2) == "F0")
     ref = ref.substr(2);

answered May 1, 2012 at 9:55

BergiBergi

588k133 gold badges915 silver badges1301 bronze badges

if it is not the first two chars and you wanna remove F0 from the whole string then you gotta use this regex

   let string = 'F0123F0456F0';
   let result = string.replace(/F0/ig, '');
   console.log(result);

answered Jan 18, 2020 at 17:14

Remove specific character in string javascript

Not the answer you're looking for? Browse other questions tagged javascript string or ask your own question.

Given a string and the task is to remove a character from the given string.

Method 1: Using replace() method: The replace method is used to replace a specific character/string with other character/string. It takes two parameters, first is the string to be replaced and the second is the string which is to be replaced with. In this case, the first parameter is the character which is to be removed and the second parameter can be given as an empty string. This will remove the character from the string. This method removes the first occurrence of the string.

Syntax:

string.replace('characterToReplace', '');

Example: 

html

<html>

<head>

    <title>

        How to remove a character from

        string using Javascript?

    title>

head>

<body>

    <h2 style="color: green">

        GeeksforGeeks

    h2>

    <b>

        How to remove a character from

        a string using Javascript?

    b>

    <p>Original string is GeeksforGeeksp>

    <p>

        New String is:

        <span class="output">span>

    p>

    <button onclick="removeCharacter()">

        Remove Character

    button>

    <script type="text/javascript">

        function removeCharacter() {

            originalString = 'GeeksForGeeks';

            newString = originalString.replace('G', '');

            document.querySelector('.output').textContent

                    = newString;

        }

    script>

body>

html>                   

Output:

  • Before clicking the button:

 

Remove specific character in string javascript

  • After clicking the button:

Remove specific character in string javascript

Method 2: Using replace() method with a regular expression: This method is used to remove all occurrences of the specified character, unlike the previous method. A regular expression is used instead of the string along with the global property. It will select every occurrence in the string and it can be removed. 

Syntax:

string.replace(/regExp/g, '');

Example: 

html

<html>

<head>

    <title>

        How to remove a character from

        string using Javascript?

    title>

head>

<body>

    <h2 style="color: green">

        GeeksforGeeks

    h2>

    <b>

        How to remove a character from

        a string using Javascript?

    b>

    <p>Original string is GeeksforGeeksp>

    <p>

        New String is:

        <span class="output">span>

    p>

    <button onclick="removeCharacter()">

        Remove Character

    button>

    <script type="text/javascript">

        function removeCharacter() {

            originalString = 'GeeksForGeeks';

            newString = originalString.replace(/G/g, '');

            document.querySelector('.output').textContent

                    = newString;

        }

    script>

body>

html>                   

Output:

  • Before clicking the button:

 

Remove specific character in string javascript

  • After clicking the button:

 

Remove specific character in string javascript

Method 3: Removing the first or last character using slice() method: The slice() method is used to extract parts of a string between the given parameters. This method takes the starting index and the ending index of the string and returns the string in between these indices. If the ending index is not specified, it is assumed to be the length of the string. The first character could be removed by specifying the beginning index to be 1. It extracts the string from the second character up to the end of the string. The last character could be removed by specifying the ending index to be one less than the length of the string. This extracts the string from the beginning of the string to the second to last character.

Syntax:

// Removing the first character
string.slice(1);

// Removing the last character
string.slice(0, string.length - 1);

Example: 

html

<html>

<head>

    <title>

        How to remove a character from

        string using Javascript?

    title>

head>

<body>

    <h2 style="color: green">

        GeeksforGeeks

    h2>

    <b>

        How to remove a character from

        a string using Javascript?

    b>

    <p>Original string is GeeksforGeeksp>

    <p>

        First character removed string:

        <span class="output1">span>

    p>

    <p>

        Last character removed string:

        <span class="output2">span>

    p>

    <button onclick="removeCharacter()">

        Remove Character

    button>

    <script type="text/javascript">

        function removeCharacter() {

            originalString = 'GeeksForGeeks';

            firstCharRemoved = originalString.slice(1);

            lastCharRemoved =

                originalString.slice(0, originalString.length - 1);

            document.querySelector('.output1').textContent

                    = firstCharRemoved;

            document.querySelector('.output2').textContent

                    = lastCharRemoved;

        }

    script>

body>

html>                   

Output:

  • Before clicking the button:

 

Remove specific character in string javascript

  • After clicking the button:

 

Remove specific character in string javascript

Method 4: Removing a particular character at given index using substr() method: This method can be used to remove a character from a particular index in the string. The substr() method is used to extract parts of a string between the given parameters. This method takes two parameters, one is the starting index and the other is the ending index of the string. The string between these indices is returned. The portion of the string before and after the character to be removed is separated and joined together. This removes the character from the specific index.

 Syntax:

string.substr(0, position - 1) + string.substr(position, string.length);

Example: 

html

<html>

<head>

    <title>

        How to remove a character from

        string using Javascript?

    title>

head>

<body>

    <h2 style="color: green">

        GeeksforGeeks

    h2>

    <b>

        How to remove a character from

        a string using Javascript?

    b>

    <p>Original string is GeeksforGeeksp>

    <p>

        New String is:

        <span class="output">span>

    p>

    <button onclick="removeCharacter(6)">

        Remove Character

    button>

    <script type="text/javascript">

        function removeCharacter(position) {

            originalString = 'GeeksForGeeks';

            newString = originalString.substr(0, position - 1)

            + originalString.substr(position, originalString.length);

            document.querySelector('.output').textContent = newString;

        }

    script>

body>

html>                   

Output:

  • Before clicking the button:

 

Remove specific character in string javascript

  • After clicking the button:

 

Remove specific character in string javascript

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.


How do I remove a specific part of a string in JavaScript?

To remove all occurrences of a substring from a string, call the replaceAll() method on the string, passing it the substring as the first parameter and an empty string as the second. The replaceAll method will return a new string, where all occurrences of the substring are removed.

How do I remove a specific character from a string?

You can also remove a specified character or substring from a string by calling the String. Replace(String, String) method and specifying an empty string (String. Empty) as the replacement.

How do you remove all characters from a string after a specific character in JavaScript?

slice() method to remove everything after a specific character, e.g. const removed = str. slice(0, str. indexOf('[')); . The slice method will return the part of the string before the specified character.

How do you remove letters from a word in JavaScript?

This can be used to remove text from either or both ends of the string..
Syntax. str.substr(start[, length]).
Example. let a = 'Hello world' console.log(a.substr(0, 5)) console.log(a.substr(6)) console.log(a.substr(4, 3)).
Output. This will give the output − Hello world o w. ... .
Syntax. str.replace(old, new).
Example. ... .
Output..