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)

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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DariusAuthor Commented:
If no solution on this will try to user Regex....
Julian HansenCommented:
Try this
var src = '00001abacd45';
var result = src.match(/0+(\w+)/)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
	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.
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

DariusAuthor Commented:

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)
Julian HansenCommented:
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

Julian HansenCommented:
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+)/);

Open in new window

Working sample here
DariusAuthor 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!
DariusAuthor Commented:
var src = '   001abc!"$%^&*()_+234  ';

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

Open in new window

Julian HansenCommented:
	var src = '   001abc!"$%^&*()_+234  ';
	var res = src.trim().match(/0*(.*)/);

Open in new window

DariusAuthor Commented:
Thanks again...

I tried something similar.  I did mistake by using trim() function...
Julian HansenCommented:
You are welcome.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.