Solved

Remove leading, trailing space and leading zeros from string

Posted on 2016-10-05
11
35 Views
Last Modified: 2016-10-05
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?
0
Comment
Question by:Darius
  • 6
  • 5
11 Comments
 

Author Comment

by:Darius
Comment Utility
If no solution on this will try to user Regex....
0
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
Comment Utility
Try this
var src = '00001abacd45';
var result = src.match(/0+(\w+)/)
console.log(result[1]);

Open in new window

1
 
LVL 51

Assisted Solution

by:Julian Hansen
Julian Hansen earned 500 total points
Comment Utility
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.
0
 

Author Comment

by:Darius
Comment Utility
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)
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
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

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
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
0
 

Author Comment

by:Darius
Comment Utility
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!
0
 

Author Comment

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

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

Open in new window

0
 
LVL 51

Assisted Solution

by:Julian Hansen
Julian Hansen earned 500 total points
Comment Utility
	var src = '   001abc!"$%^&*()_+234  ';
	var res = src.trim().match(/0*(.*)/);

Open in new window

0
 

Author Comment

by:Darius
Comment Utility
Thanks again...

I tried something similar.  I did mistake by using trim() function...
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
You are welcome.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now