One line replace for multiple targets?

N

Noozer

Writing some ASP code and I need to take a string, representing a phone
number, and strip out some characters, namely spaces, brackets and hyphens.

I could write

pNum=replace(pNum," ","")
pNum=replace(pNum,"-","")
pNum=replace(pNum,"(","")
pNum=replace(pNum,")","")

....but that's a bit of a pain. Especially since I'll be doing similar
replacements with other data and may have to strip out a larger number of
characters.

I know that this can be done with regular expressions, but all the examples
I see online are several lines in length. Isn't there a simple way to just
say "remove any of these" or "replace any of these with nothing" in a single
line of code?
 
B

Bob Barrows [MVP]

Noozer said:
Writing some ASP code and I need to take a string, representing a
phone number, and strip out some characters, namely spaces, brackets
and hyphens.
I could write

pNum=replace(pNum," ","")
pNum=replace(pNum,"-","")
pNum=replace(pNum,"(","")
pNum=replace(pNum,")","")

...but that's a bit of a pain. Especially since I'll be doing similar
replacements with other data and may have to strip out a larger
number of characters.

I know that this can be done with regular expressions, but all the
examples I see online are several lines in length. Isn't there a
simple way to just say "remove any of these" or "replace any of these
with nothing" in a single line of code?
No
Sorry
 
M

McKirahan

Noozer said:
Writing some ASP code and I need to take a string, representing a phone
number, and strip out some characters, namely spaces, brackets and hyphens.

I could write

pNum=replace(pNum," ","")
pNum=replace(pNum,"-","")
pNum=replace(pNum,"(","")
pNum=replace(pNum,")","")

...but that's a bit of a pain. Especially since I'll be doing similar
replacements with other data and may have to strip out a larger number of
characters.

I know that this can be done with regular expressions, but all the examples
I see online are several lines in length. Isn't there a simple way to just
say "remove any of these" or "replace any of these with nothing" in a single
line of code?

If you only want digits then here's one approach.

<html>
<head>
<title>numbonly.htm</title>
<script type="text/javascript">
function numbonly(that) {
var numb = that.value;
var what = "";
for (var i=0; i<numb.length; i++) {
var byte = numb.substr(i,1);
if (byte >= "0" && byte <= "9") what += byte;
}
that.value = what;
}
</script>
</head>
<body>
<center>
<form name="form1">
<input type="text" name="numb" size="20" maxlength="20"
value="(123) 456-7890" onblur="numbonly(this)">
</form>
</center>
</body>
</html>
 
A

Anthony Jones

I know that this can be done with regular expressions, but all the examples
I see online are several lines in length. Isn't there a simple way to just
say "remove any of these" or "replace any of these with nothing" in a single
line of code?
<<

As already stated no there isn't but yet there is; use a function (the art
of abstraction is often
lost in the serial nature of ASP script).

Have the following function (and a range of other useful utilities) in an
ASP page that is included in the pages where you need them:-

Function RegExReplace(rsInput, rsPattern, rsReplace)

Dim oRegEx

Set oRegEx = New RegExp

oRegEx.Pattern = rsPattern
oRegEx.Global = True
RegExReplace = oRegEx.Replace(rsInput, rsReplace)

End Function

Now your stated case becomes:-

pNum = RegExReplace(pNum;" |-|\)|\)","")

and your other similar cases will be simply a matter of varing the pattern.


PS. where is 'microsoft.public.inetserver.iis.activeserverpages' that you
cross posted to?
my newsgroup app complains that is doesn't exist

Anthony.
 
M

McKirahan

[snip]
PS. where is 'microsoft.public.inetserver.iis.activeserverpages' that you
cross posted to? my newsgroup app complains that is doesn't exist

"microsoft.public.inetserver.iis.activeserverpages"
exists and has had 588 posts since January 1, 2004.
 
A

Andyza

Bob, it's very rare, but this time you're wrong. See the message in
this thread by Anthony Jones. A Regular Expression is ideal for this
problem.
 
B

Bob Barrows [MVP]

Andyza said:
Bob, it's very rare, but this time you're wrong. See the message in
this thread by Anthony Jones. A Regular Expression is ideal for this
problem.

Oh wait! I AM wrong. Nested Replace statements will accomplish this on a
single line.

This
pNum=replace(pNum," ","")
pNum=replace(pNum,"-","")
pNum=replace(pNum,"(","")
pNum=replace(pNum,")","")

can be done by:
pnum=replace(replace(replace(replace(pNum,")",""),"(",""),"-","")," ","")
 
B

Bob Barrows [MVP]

Andyza said:
Bob, it's very rare, but this time you're wrong. See the message in
this thread by Anthony Jones. A Regular Expression is ideal for this
problem.
What are you talking about? Please quote the message to which you are
replying.
Oh wait, I remember now.

A Regular Expession may be ideal for this problem, but a regular expression
also takes at least 3 lines to use, one to instantiate the regexp object,
one to initialize the pattern, and one to execute the replace statement. So
it is not the answer to the question you asked.
 
E

Evertjan.

Noozer wrote on 03 feb 2006 in microsoft.public.inetserver.asp.general:
Writing some ASP code and I need to take a string, representing a
phone number, and strip out some characters, namely spaces, brackets
and hyphens.

I could write

pNum=replace(pNum," ","")
pNum=replace(pNum,"-","")
pNum=replace(pNum,"(","")
pNum=replace(pNum,")","")

...but that's a bit of a pain. Especially since I'll be doing similar
replacements with other data and may have to strip out a larger number
of characters.

I know that this can be done with regular expressions, but all the
examples I see online are several lines in length. Isn't there a
simple way to just say "remove any of these" or "replace any of these
with nothing" in a single line of code?

execute function in vbs or js:
<%
pNum = pNumReplace(pNum)
%>

define in jscript:
<script runat=server language=jscript>
function pNumReplace(x) { return x.replace(/[ -()]+/g,'') }
</script>
 
A

Andyza

Now your stated case becomes:-
pNum = RegExReplace(pNum;" |-|\)|\)","")

and your other similar cases will be simply a matter of varing the pattern.

Maybe I'm reading it wrong, but shouldn't that be:

pNum = RegExReplace(pNum," |-|\(|\)","")

i.e.: " |-|\(|\)" instead of " |-|\)|\)"

The original example has two closing brackets instead of one opening
and one closing bracket.

And shouldn't there be a comma after pNum instead of a semicolon?
 
A

Anthony Jones

i.e.: " |-|\(|\)" instead of " |-|\)|\)"
The original example has two closing brackets instead of one opening
and one closing bracket.
And shouldn't there be a comma after pNum instead of a semicolon?

Your quite right thats a typo.

Should read:-

pNum = RegExReplace(pNum," |-|\(|\)","")


Another option would be:-


Function CharReplace(rsInput, rsChars, rsReplace)

Dim i

CharReplace = rsInput

For i = 1 To Len(rsChars)
CharReplace = Replace(CharReplace, Mid(rsChars, i, 1), rsReplace)
Next

End Function

pNum = CharReplace(pNum, " -()", "")

Personally I prefer the RegExp solution since I don't like to create a
destory strings if I can help it. But the above may perform better for
short rsChars.

My main point is that there is nearly all ways a single line solution.
Create a function.

Anthony.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top