一行代替多个目标? [英] One line replace for multiple targets?
问题描述
编写一些ASP代码,我需要一个字符串,代表一个手机
数字,并删除一些字符,即空格,括号和连字符。
我可以写
pNum = replace(pNum,"","")
pNum = replace(pNum," - ","")
pNum = replace(pNum,"(","")
pNum = replace(pNum," ),,
....但这有点痛苦。特别是因为我会用其他数据做类似的
替换,可能需要删除更多的
字符。
我知道这可以用正则表达式来完成,但我在网上看到的所有例子都是几行的长度。难道不是一个简单的方法,只需要说明删除其中任何一个。或用任何东西替换任何这些在单个
行代码中?
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?
推荐答案
Noozer写道:
写一些ASP代码,我需要一个字符串,代表一个
电话号码,并删除一些字符,即空格,括号
和连字符。
我可以写
pNum = replace(pNum,"","")
pNum = replace(pNum," - ","")
pNum = replace(pNum,"( ","")
pNum = replace(pNum,")","")
......但这有点痛苦。特别是因为我会用其他数据进行类似的替换,可能需要删除更多的字符。
我知道这可以通过常规来完成表达式,但我在网上看到的所有例子都有几行。难道只是说删除其中任何一个的简单方法吗?或用任何东西替换任何这些
在一行代码中?
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?
否
抱歉
-
Microsoft MVP - ASP / ASP.NET
请回复新闻组。这个电子邮件帐户是我的垃圾邮件陷阱所以我
不经常检查它。如果您必须离线回复,请删除
NO SPAM
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"
" Noozer" <做******* @ me.here>在消息中写道
news:2xDEf.315369
"Noozer" <do*******@me.here> wrote in message
news:2xDEf.315369
tl.211704@pd7tw3no ...
tl.211704@pd7tw3no...
编写一些ASP代码,我需要拿一个字符串,代表一个电话号码,并删掉一些字符,即空格,括号和
连字符。
我可以写
pNum = replace( pNum,","")
pNum = replace(pNum," - ","")
pNum = replace(pNum,"(",") ;")
pNum = replace(pNum,")","")
......但这有点痛苦。特别是因为我会用其他数据进行类似的替换,并且可能需要删除更多的
字符。
我知道这可以通过常规来完成表达式,但我在网上看到的所有
示例都有几行。难道没有简单的方法可以说删除其中任何一个。或用任何东西替换任何这些在
的单行代码中?
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?
如果你只想要数字,那么这是一种方法。
< html>
< head>
< title> numbonly.htm< / title>
< script type = " text / javascript">
函数numbonly(那){
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" ;命名= QUOT;麻木"大小= QUOT; 20" maxlength =" 20"
value ="(123)456-7890" onblur =" numbonly(this)">
< / form>
< / center>
< / body> ;
< / html>
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>
这篇关于一行代替多个目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!