Solved

Remove leading, trailing space and leading zeros from string

Posted on 2016-10-05
11
45 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
ID: 41829831
If no solution on this will try to user Regex....
0
 
LVL 54

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 54

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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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 54

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 54

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 54

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 54

Expert Comment

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

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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…

770 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