Remove leading, trailing space and leading zeros from string

Darius
Darius used Ask the Experts™
on
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)

var invoiceId = parseInt(document.getElementById("txtId").value) || "";

Open in new window

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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
If no solution on this will try to user Regex....
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
Try this
var src = '00001abacd45';
var result = src.match(/0+(\w+)/)
console.log(result[1]);

Open in new window

Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
Alternative
	var temp = src.trim();
	var i = 0;
	while(temp[i] == '0' && i++ < temp.length);
	var result = temp.substring(i);

Open in new window

First one is about 4x faster.

Author

Commented:
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)
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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_]+)/
	var result = src.match(/0+([\w_]+)/);

Open in new window

Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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);

Open in new window

Working sample here

Author

Commented:
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.

var src = '   001abc!"$%^&*()_+234  ';
// result:  '1abc!"$%^&*()_+234

Open in new window

'

Thank you!

Author

Commented:
var src = '   001abc!"$%^&*()_+234  ';

// result:  '1abc!"$%^&*()_+234'

Open in new window

Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
	var src = '   001abc!"$%^&*()_+234  ';
	var res = src.trim().match(/0*(.*)/);

Open in new window

Author

Commented:
Thanks again...

I tried something similar.  I did mistake by using trim() function...
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You are welcome.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial