Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove specific characters from a string in 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(); 
like image 494
AlphaPapa Avatar asked May 01 '12 09:05

AlphaPapa


People also ask

How do I remove a specific character from a string?

Using 'str. replace() , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.

How do you remove all occurrences of a character from a string in JavaScript?

Delete all occurrences of a character in javascript string using replaceAll() The replaceAll() method in javascript replaces all the occurrences of a particular character or string in the calling string. The first argument: is the character or the string to be searched within the calling string and replaced.

How do you trim a character in JavaScript?

JavaScript provides three functions for performing various types of string trimming. The first, trimLeft() , strips characters from the beginning of the string. The second, trimRight() , removes characters from the end of the string. The final function, trim() , removes characters from both ends.

How do you remove a substring from a string?

To remove a substring from a string, call the replace() method, passing it the substring and an empty string as parameters, e.g. str. replace("example", "") . The replace() method will return a new string, where the first occurrence of the supplied substring is removed.


1 Answers

Simply replace it with nothing:

var string = 'F0123456'; // just an example string.replace(/^F0+/i, ''); '123456' 
like image 172
Mathias Bynens Avatar answered Sep 19 '22 14:09

Mathias Bynens