By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,665 Members | 1,373 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,665 IT Pros & Developers. It's quick & easy.

One line replace for multiple targets?

P: n/a
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?
Feb 3 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Noozer wrote:
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
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Feb 3 '06 #2

P: n/a
"Noozer" <do*******@me.here> wrote in message
news:2xDEf.315369$tl.211704@pd7tw3no...
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>
Feb 3 '06 #3

P: n/a
>>
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.
Feb 4 '06 #4

P: n/a
"Anthony Jones" <An*@yadayadayada.com> wrote in message
news:#h**************@tk2msftngp13.phx.gbl...

[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.
Feb 4 '06 #5

P: n/a
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.

Feb 16 '06 #6

P: n/a
Andyza wrote:
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,")",""), "(",""),"-","")," ","")

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Feb 16 '06 #7

P: n/a
Andyza wrote:
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.

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Feb 16 '06 #8

P: n/a
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>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 16 '06 #9

P: n/a
> 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?

Feb 16 '06 #10

P: n/a
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.

Feb 16 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.