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[];
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
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
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.
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
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
How to remove a character from
string using Javascript?
GeeksforGeeks
How to remove a character from
a string using Javascript?
Original string is GeeksforGeeks
New String is:
Remove Character
function removeCharacter[] {
originalString = 'GeeksForGeeks';
newString = originalString.replace['G', ''];
document.querySelector['.output'].textContent
= newString;
}
Output:
- Before clicking the button:
- After clicking the button:
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
How to remove a character from
string using Javascript?
GeeksforGeeks
How to remove a character from
a string using Javascript?
Original string is GeeksforGeeks
New String is:
Remove Character
function removeCharacter[] {
originalString = 'GeeksForGeeks';
newString = originalString.replace[/G/g, ''];
document.querySelector['.output'].textContent
= newString;
}
Output:
- Before clicking the button:
- After clicking the button:
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
How to remove a character from
string using Javascript?
GeeksforGeeks
How to remove a character from
a string using Javascript?
Original string is GeeksforGeeks
First character removed string:
Last character removed string:
Remove Character
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;
}
Output:
- Before clicking the button:
- After clicking the button:
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
How to remove a character from
string using Javascript?
GeeksforGeeks
How to remove a character from
a string using Javascript?
Original string is GeeksforGeeks
New String is:
Remove Character
function removeCharacter[position] {
originalString = 'GeeksForGeeks';
newString = originalString.substr[0, position - 1]
+ originalString.substr[position, originalString.length];
document.querySelector['.output'].textContent = newString;
}
Output:
- Before clicking the button:
- After clicking the button:
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.