...

View Full Version : I really need some help with Javascript for a possible project. Thank You ahead of it



BeeGinger
12-16-2011, 01:17 AM
So I feel like I should just explain what I am looking to do. Lets say there is an Item that has the number 001234-A1. This Number is the exact number that the manufacturer lists it is, however whenever it gets placed into other catalogs or websites the item number changes slightly for example:

12-34a1
0001234A1
1-234-A1
123-4a1

So what I would like to have someone show me how to do it write a script that can do three things

Remove all leading zero's
Remove all hyphens
Make all letters uppercase.

This way we now have a normalized number (1234A1).
So to recap I need to create a string that takes any different numbers, normalizes those numbers, and then matches them with all other matching numbers.
I really would appreciate some assistance in this. Thank You, Thank You

Dormilich
12-16-2011, 02:06 AM
- remove leading zeroes: use String.replace() with a RegExp
- remove hyphens: same function, though no RegExp required
- use String.toUpperCase()

Old Pedant
12-16-2011, 02:09 AM
Easy.



function AlphaNumOnly( str )
{
return str.replace(/[^a-z0-9]/g,"").toUpperCase();
}

alert( AlphaNumOnly(" 12-34a1 ") ); // with spaces
alert( AlphaNumOnly("0001234A1") );
alert( AlphaNumOnly("$.@!&*;:1-234-A1") ); // other characters thrown in
alert( AlphaNumOnly("123-4a1") );

Philip M
12-16-2011, 08:44 AM
Easy.



function AlphaNumOnly( str )
{
return str.replace(/[^a-z0-9]/g,"").toUpperCase();
}

alert( AlphaNumOnly(" 12-34a1 ") ); // with spaces
alert( AlphaNumOnly("0001234A1") );
alert( AlphaNumOnly("$.@!&*;:1-234-A1") ); // other characters thrown in
alert( AlphaNumOnly("123-4a1") );


Needs slight improvement to strip leading zeroes as reqested:-


function AlphaNumOnly( str ) {
str = str.replace(/[^0-9a-z]/gi,"").toUpperCase();
str = str.replace(/^0+/,"");
return str;
}

Old Pedant
12-16-2011, 11:38 PM
Told you I'm going blind as well as senile. Flat out missed that requirement.

cuzMazn
12-17-2011, 08:05 AM
Needs slight improvement to strip leading zeroes as reqested:-


function AlphaNumOnly( str ) {
str = str.replace(/[^0-9a-z]/gi,"").toUpperCase();
str = str.replace(/^0+/,"");
return str;
}

Hi,

That won't work if the item has special characters, he didn't say that special characters were not allowed. So let's do what he said:



function format($string) {
//Remove all leading zeros
while (parseInt($string[0]) === 0) {
$string = $string.substr(1, $string.length - 1);
};

//Remove all hyphens
$string = $string.split("-").join("");

//Make all letters uppercase
return $string.toUpperCase();
};


Test:



var $code = "000123-4A-1";
document.write(format($code));


Result:



1234A1


I hope this help.

Tim_

Philip M
12-17-2011, 08:33 AM
Hi,

That won't work if the item has special characters, he didn't say that special characters were not allowed. So let's do what he said:



My understanding of the OP's post #1 is that the manufacturer's item code consists of letters, numbers and hyphens. Special characters and spaces were not mentioned. Perhaps the OP can confirm. But if that is really what is wanted then only a slight alteration to the code is needed.



function AlphaNumOnly( str ) {
str = str.replace(/-/gi,"").toUpperCase(); // strip hyphens only, not spaces or special characters
str = str.replace(/^0+/,"");
return str;
}

Dormilich
12-17-2011, 01:05 PM
I would even go as far as proposing

function AlphaNumOnly( str ) {
return str.replace(/^0+/, "").replace("-", "").toUpperCase();
}

Philip M
12-17-2011, 01:20 PM
I would even go as far as proposing

function AlphaNumOnly( str ) {
return str.replace(/^0+/, "").replace("-", "").toUpperCase();
}

Try

alert (AlphaNumOnly("----0000abc123")); // ---ABC123 (just strips one hyphen)


function AlphaNumOnly2( str ) {
str = str.replace(/-/gi,"").toUpperCase(); // strip hyphens only, not spaces or special characters
str = str.replace(/^0+/,"");
return str;
}

alert (AlphaNumOnly2("----0000abc123")); // ABC123

Dormilich
12-17-2011, 01:32 PM
my bad, just looked it up, last time I used .replace("-", "", "g") which is equivalent to using a RegExp.

Philip M
12-17-2011, 04:29 PM
Still does not wok. And I have to confess that I am not familiar with that notation.


return str.replace(/^0+/, "").replace(/-/g, "").toUpperCase(); // 0000ABC1123

One-liners may be elegant but are harder to understand, and prone to errors. :p

Dormilich
12-17-2011, 04:38 PM
depends on which string you use. if you take ----0000abc123 then it won’t work unless you first strip the hyphens, which will make then the zeroes leading (otherwise they are in the middle), hence using str.replace(/-/g, "").replace(/^0+/, "").toUpperCase() succeeds.

regarding that one-liner, it’s just chaining, nothing more, nothing less.

cuzMazn
12-17-2011, 09:00 PM
depends on which string you use. if you take ----0000abc123 then it wonít work unless you first strip the hyphens, which will make then the zeroes leading (otherwise they are in the middle), hence using str.replace(/-/g, "").replace(/^0+/, "").toUpperCase() succeeds.

regarding that one-liner, itís just chaining, nothing more, nothing less.

Hi,

I don't think that is correct. If I understand the description, it means that the zeros in "---000abc123" is no longer leading zeros, they are now part of the code. Beeginger should clarify it.

Tim_

venegal
12-17-2011, 09:40 PM
I don't think that is correct. If I understand the description, it means that the zeros in "---000abc123" is no longer leading zeros, they are now part of the code. Beeginger should clarify it.


The OP states that this is about normalizing different number formats that treat hyphens as non-semantic, so hyphens are clearly not allowed to change leading- to non-leading-zeroes.

Some catalog might choose to represent 001234-A1 as 0-0-1234-A1, but the normalized form is still supposed to be 1234A1, and not 01234A1.

And since we're having fun with hard to maintain regexps, here's mine:

str.replace(/^[0-]*|-/g, '').toUpperCase();



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum