AnA*_*ice 450 javascript regex jquery
给出如下字符串:
"The dog has a long tail, and it is RED!"
什么样的jQuery或JavaScript魔法可用于将空间最多只保留一个空间?
目标:
"The dog has a long tail, and it is RED!"
Bal*_*usC 835
鉴于您还想要覆盖制表符,换行符等,只需替换\s\s+为' ':
string = string.replace(/\s\s+/g, ' ');
Run Code Online (Sandbox Code Playgroud)
如果你真的想要只覆盖空格(因此不是选项卡,换行符等),请执行以下操作:
string = string.replace(/ +/g, ' ');
Run Code Online (Sandbox Code Playgroud)
Edw*_*per 156
由于你似乎对性能感兴趣,我用firebug描述了这些.以下是我得到的结果:
str.replace( / +/g, ' ' ) -> 380ms
str.replace( /\s\s+/g, ' ' ) -> 390ms
str.replace( / {2,}/g, ' ' ) -> 470ms
str.replace( / +/g, ' ' ) -> 790ms
str.replace( / +(?= )/g, ' ') -> 3250ms
Run Code Online (Sandbox Code Playgroud)
这是在Firefox上,运行100k字符串替换.
如果您认为性能是一个问题,我鼓励您使用firebug进行自己的性能测试.众所周知,人类在预测项目瓶颈所处的位置方面是不好的.
(另请注意,IE 8的开发人员工具栏中还内置了一个分析器 - 可能值得检查IE中的性能.)
wat*_*ain 43
var str = "The dog has a long tail, and it is RED!";
str = str.replace(/ {2,}/g,' ');
Run Code Online (Sandbox Code Playgroud)
编辑: 如果你想替换所有类型的空白字符,最有效的方式是这样的:
str = str.replace(/\s{2,}/g,' ');
Run Code Online (Sandbox Code Playgroud)
med*_*iev 16
这是一个解决方案,但它将针对所有空格字符:
"The dog has a long tail, and it is RED!".replace(/\s\s+/g, ' ')
"The dog has a long tail, and it is RED!"
Run Code Online (Sandbox Code Playgroud)
编辑:这可能更好,因为它的目标是一个空格后跟一个或多个空格:
"The dog has a long tail, and it is RED!".replace(/ +/g, ' ')
"The dog has a long tail, and it is RED!"
Run Code Online (Sandbox Code Playgroud)
替代方法:
"The dog has a long tail, and it is RED!".replace(/ {2,}/g, ' ')
"The dog has a long tail, and it is RED!"
Run Code Online (Sandbox Code Playgroud)
我没有/\s+/单独使用,因为它取代了多次跨越1个字符的空格,并且可能效率较低,因为它的目标超出了必要的范围.
如果有错误,我没有深入测试任何这些lmk.
此外,如果您要进行字符串替换,请记住将变量/属性重新分配给自己的替换,例如:
var string = 'foo'
string = string.replace('foo', '')
Run Code Online (Sandbox Code Playgroud)
使用jQuery.prototype.text:
var el = $('span:eq(0)');
el.text( el.text().replace(/\d+/, '') )
Run Code Online (Sandbox Code Playgroud)
Nen*_*lep 13
我有这个方法,我称之为Derp方法缺乏一个更好的名字.
while (str.indexOf(" ") !== -1) {
str = str.replace(/ /g, " ");
}
Run Code Online (Sandbox Code Playgroud)
Eth*_*han 13
一个更健壮的方法:如果它们存在,它还负责删除初始和尾随空格.例如:
// NOTE the possible initial and trailing spaces
var str = " The dog has a long tail, and it is RED! "
str = str.replace(/^\s+|\s+$|\s+(?=\s)/g, "");
// str -> "The dog has a long tail, and it is RED !"
Run Code Online (Sandbox Code Playgroud)
您的例子没有这些空间,但他们是一个非常常见的情况也和接受的答案只是修剪那些成单个空格,如:"在... RED",这是不是你通常需要.
小智 11
更强大:
function trim(word)
{
word = word.replace(/[^\x21-\x7E]+/g, ' '); // change non-printing chars to spaces
return word.replace(/^\s+|\s+$/g, ''); // remove leading/trailing spaces
}
小智 8
我建议
string = string.replace(/ +/g," ");
Run Code Online (Sandbox Code Playgroud)
只是空格
或
string = string.replace(/(\s)+/g,"$1");
Run Code Online (Sandbox Code Playgroud)
用于将多个退货转换为单个退货.
如果您不想使用replace(在不使用replace javascript的情况下替换字符串中的空格),这是一个替代解决方案
var str="The dog has a long tail, and it is RED!";
var rule=/\s{1,}/g;
str = str.split(rule).join(" ");
document.write(str);
Run Code Online (Sandbox Code Playgroud)
我知道我迟到了,但我发现了一个很好的解决方案.
这里是:
var myStr = myStr.replace(/[ ][ ]*/g, ' ');
Run Code Online (Sandbox Code Playgroud)
小智 5
newbies等人的全面未加密答案.
这适用于像我这样的所有虚拟测试人员,他们会测试一些不能工作的人编写的脚本.
以下3个示例是我在以下3个网站上删除特殊字符和额外空格的步骤(所有这些都完美无缺){1.EtaVisa.com 2. EtaStatus.com 3. Tikun.com}所以我知道这些工作完美.
我们将这些链接在一起超过50个,没有任何问题.
//这删除了特殊字符+ 0-9并且只允许字母(大写和LOWER大小写)
function NoDoublesPls1()
{
var str=document.getElementById("NoDoubles1");
var regex=/[^a-z]/gi;
str.value=str.value.replace(regex ,"");
}
Run Code Online (Sandbox Code Playgroud)
//这删除了特殊字符,只允许字母(大写和LOWER大小写)和0-9 AND空格
function NoDoublesPls2()
{
var str=document.getElementById("NoDoubles2");
var regex=/[^a-z 0-9]/gi;
str.value=str.value.replace(regex ,"");
}
Run Code Online (Sandbox Code Playgroud)
//这删除了特殊字符,只允许字母(大写和LOWER大小写)和0-9 AND空格//末尾的.replace(/\s\s +/g,"")删除多余的空格//当我使用单引号,它没有用.
function NoDoublesPls3()
{ var str=document.getElementById("NoDoubles3");
var regex=/[^a-z 0-9]/gi;
str.value=str.value.replace(regex ,"") .replace(/\s\s+/g, " ");
}
Run Code Online (Sandbox Code Playgroud)
:: NEXT ::
将#3保存为a .js//我打电话给我的NoDoubles.js
:: NEXT :: 将您的JS包含在您的页面中
<script language="JavaScript" src="js/NoDoubles.js"></script>
Run Code Online (Sandbox Code Playgroud)
将其包含在您的表单字段::例如
<INPUT type="text" name="Name"
onKeyUp="NoDoublesPls3()" onKeyDown="NoDoublesPls3()" id="NoDoubles3"/>
Run Code Online (Sandbox Code Playgroud)
所以它看起来像这样
<INPUT type="text" name="Name" onKeyUp="NoDoublesPls3()" onKeyDown="NoDoublesPls3()" id="NoDoubles3"/>
Run Code Online (Sandbox Code Playgroud)
这将删除特殊字符,允许单个空格并删除多余的空格.
| 归档时间: |
|
| 查看次数: |
388517 次 |
| 最近记录: |