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();
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.
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.
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.
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.
Simply replace it with nothing:
var string = 'F0123456'; // just an example string.replace(/^F0+/i, ''); '123456'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With