# Convert this function from asp to js

hello, i have a function that was written out for me in asp. i am trying to convert it to js but am getting confused with the double IF syntax, can somebody assist me in converting this to javascript:

``````
D = "W1K 1BE"
DL = Instr(D," ")
Dleft = left(D,DL - 1)
Dleft2 = left(Dleft,2)
Dright2 = mid(Dleft,3)
Dright2len = len(Dright2)
i = 1
Dright2end = Dright2

Do While i <= Dright2len
strcheck = mid(Dright2,i,1)

if IsNumeric(strcheck) 	then
Dright2end = Dright2end
else

strcheckexists = Instr(Dleft2,strcheck)
if strcheckexists = 0 then

Dright2end = Replace(Dright2end,strcheck,"")
end if
end if
i = i +1
Loop

strfinal = Dleft2 &  Dright2end

``````

thanks

It seems to be a nested if statement.

Here’s how the structure should be:

``````
do {
...
if (IsNumeric(strcheck)) {
...
} else {
...
if (strcheckexists = 0) {
...
}
}
...
} while (i <= Dright2len);

``````

This code started out as a direct conversion of the script, then went through a series of reductions to end up with the following:

``````
function convert(str) {
var parts = str.split(' '),
chars = parts.match(/\\D/g).join(''), // non-numeric chars
re = new RegExp('[^\\\\d' + chars + ']', 'g'); // anything that is not (a digit or one of the chars)
return parts + parts.replace(re, '');
}

var converted = convert('W1K 1BE');
// converted is 'W1K1'

``````

The end result is the same, where any letters in the second part are removed if they’re not in the first part.

hello, thank you very much for that response. i still have not got this working and am becoming very confused. i think the way my page has been coded is that the javascript is held within the asp file…

here is my code (i have removed a lot of surplus code)

``````
<%@ Language = "JavaScript" %>
<%

Response.ContentType = "text/html";

var P = "";

if (Request.Form("P").Count == 1)
{
P = String(Request.Form("P"));
}
else if (Request.QueryString("P").Count == 1)
{
P = String(Request.QueryString("P"));
}

%>

<h1>[B]<% Response.Write(P); %>[/B] to Heathrow Airport</h1>

``````

i have highlighted in bold where my postcode displays. the value that i am trying to manipulate is P

what i need help with now is integrating that function into this code and i would also like to clarify the rules again of the manipulation…

NW1A 1AA

first rule - remove all values after and including the space

NW1A

second rule - remove all alphabetical characters that are not in the first 2 characters of the value

NW1

that’s it. i would need this rule to be applied to my P value and if someone can match the syntax of my current code i shall try it out.

thanks again and look forward to a response

Oh well thats good. Now that we have the rules we can create a regular expression that does the work for us.

The two letters could be either together or apart.

If as I understand it, these are UK post codes, they won’t start with a number, so the worst case could be A34B56. We can capture that using a regular expression where the numbers on either side can be optional too.

So [A-Z]\d*[A-Z]\d* is the regular expression that gets a letter followed by 0 or more digits, then another letter followed by 0 or more digits.

However, as these are UK postcodes, you should be able to get away with matching one or more consecutive digits, followed by one or more digits

[A-Z]+\d+

So we can put that within parenthesis as a capture group, and place a carat (^) at the start to ensure that the matches start from the beginning.

^([A-Z]+\d+)

Finally, we put forward slashes on either side, and that’s your regular expression.

``````
var rx = /(^[A-Z]+\\d+)/;

``````

Let’s test it out.

``````
var postcode = 'NW1A 1AA';
var rx = /(^[A-Z]+\\d+)/;
var result = rx.exec(postcode);
area = result;
// area is 'NW1'

``````

So as a javascript function that would be:

``````
function postcodeArea(postcode) {
var rx = /(^[A-Z]+\\d+)/;
var result = rx.exec(postcode);
return result;
}

``````

The code in your example is all ASP code, which all runs before the result is sent provided as a web page.
JavaScript on the other hand runs on the client side after the web page has been sent.

You should find though that the regular expression can be used in the same way, it’s just the implementation details of ASP which differ.

hi again, i am using the following code:

``````
<%
var postcode = P
var rx = /(^\\d*[A-Z]\\d*[A-Z]\\d*)/;
var result = rx.exec(postcode);
area = result;

%>

<h1><% Response.Write(area); %> to Heathrow Airport</h1>

``````

and am getting this error:

[b]Microsoft JScript runtime error ‘800a138f’

‘result.1’ is null or not an object [/b]

This is the JavaScript forum, and I don’t know the first thing about ASP.

Do you want to move this thread to our Classic ASP forum?