Solved

Remove leading, trailing space and leading zeros from string

Posted on 2016-10-05
11
69 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 

Author Comment

by:Darius
ID: 41829831
If no solution on this will try to user Regex....
0
 
LVL 58

Accepted Solution

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

Open in new window

1
 
LVL 58

Assisted Solution

by:Julian Hansen
Julian Hansen earned 500 total points
ID: 41829929
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Darius
ID: 41829946
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 58

Expert Comment

by:Julian Hansen
ID: 41829981
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
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 41830004
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
ID: 41830080
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
ID: 41830082
var src = '   001abc!"$%^&*()_+234  ';

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

Open in new window

0
 
LVL 58

Assisted Solution

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

Open in new window

0
 

Author Comment

by:Darius
ID: 41830174
Thanks again...

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

Expert Comment

by:Julian Hansen
ID: 41830205
You are welcome.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
Suggested Courses

622 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