Darius
asked on
Remove leading, trailing space and leading zeros from string
Hi guys,
I'm using 'parseInt' function to
// Removes all leading and trailing white-space characters from the string and then
// Removes all leading occurrences of the specified character 0 (zeros)
output result: invoiceId ="1" // 'parseInt' function removes all alpha charters
output required: invoiceId ="1abc2345"
Any other way to remove leading and trailing white-space and leading zeros?
I'm using 'parseInt' function to
// Removes all leading and trailing white-space characters from the string and then
// Removes all leading occurrences of the specified character 0 (zeros)
var invoiceId = parseInt(document.getElementById("txtId").value) || "";
input: invoiceId = " 0001abc2345 ";output result: invoiceId ="1" // 'parseInt' function removes all alpha charters
output required: invoiceId ="1abc2345"
Any other way to remove leading and trailing white-space and leading zeros?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Julian,
it works for :
var src = '00001abacd45';
result: '1abacd45'
This one gives incorrect result:
var src = '00001aba$_cd45';
result: '1aba'
Acceptation for alphanumeric with one additional character (hyphen)
it works for :
var src = '00001abacd45';
result: '1abacd45'
This one gives incorrect result:
var src = '00001aba$_cd45';
result: '1aba'
Acceptation for alphanumeric with one additional character (hyphen)
You need to let us know what the data requirements are - your original example did not have an underscore
just change the pattern to /0+([\w_]+)/
just change the pattern to /0+([\w_]+)/
var result = src.match(/0+([\w_]+)/);
Belay that the original was fine as it was. Underscore is included in the \w match.
var src = ' 00001abac_d45 ';
var result = src.match(/0+(\w+)/);
console.log(result);
Working sample here
ASKER
Thank you! Working!!!
Now I thinking another situation:
How to remove leading trailing white-space and leading zeros only. Other numbers, alphabetic characters and any possible characters to leave as it is.
Thank you!
Now I thinking another situation:
How to remove leading trailing white-space and leading zeros only. Other numbers, alphabetic characters and any possible characters to leave as it is.
var src = ' 001abc!"$%^&*()_+234 ';
// result: '1abc!"$%^&*()_+234
'Thank you!
ASKER
var src = ' 001abc!"$%^&*()_+234 ';
// result: '1abc!"$%^&*()_+234'
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks again...
I tried something similar. I did mistake by using trim() function...
I tried something similar. I did mistake by using trim() function...
You are welcome.
ASKER