jquery .replace(/./g, "") 不适合我,但其他人

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16970237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:10:31  来源:igfitidea点击:

jquery .replace(/./g, "") do not work for me but others

javascriptjqueryreplacepreg-replace

提问by hamburger

I found this snippet somewhere and it works like a charm:

我在某处找到了这个片段,它的作用就像一个魅力:

var n = parseInt(e.find("span.favNum").text().replace(/./g, "")) + 1;

If I do it in a similar way it doesn't work anymore.

如果我以类似的方式这样做,它就不再起作用了。

I do the following:

我执行以下操作:

<div id ="test">6.987</div>
var test = $("#test");
var r = test.text().replace(/./g, "");
console.log("wrong ", r);

I know that I can replace it also like this:

我知道我也可以像这样替换它:

var r = test.text().replace(".", "");

This works.

这有效。

I would like to understand why the "stolen" snippet is working. Any idea?

我想了解为什么“被盗”片段有效。任何的想法?

http://jsfiddle.net/nJZMf/3/

http://jsfiddle.net/nJZMf/3/

The original script is found here: http://wp-svbtle.themeskult.com/

原始脚本在这里找到:http: //wp-svbtle.themeskult.com/

You will find the snippet by viewing the source of index.html and searching for .replace.

您将通过查看 index.html 的源代码并搜索.replace.

采纳答案by Michael Geary

The reason that the code in the page you linked to works, where yours doesn't, is that it's not the same regular expression. Here's what I found in that page (and similar code in several places)

您链接到的页面中的代码有效而您的无效的原因是它不是同一个正则表达式。这是我在该页面中找到的内容(以及几个地方的类似代码)

r = n.text().replace( /,/g, "" )

where ris a jQuery object.

rjQuery 对象在哪里。

Note that the regular expression has a ,inside the //, not a .like the code you had trouble with.

请注意,正则表达式在,内部有一个//,而不是.像您遇到问题的代码那样。

Comma is not a special character in regular expressions, so it needs no special treatment. Period has a special meaning. As the other answers pointed out, it matches allcharacters, and you need to prefix it with \if you want to match .only.

逗号在正则表达式中不是特殊字符,所以不需要特殊处理。句号有特殊的意义。正如其他答案所指出的那样,它匹配所有字符,\如果您只想匹配,则需要在它前面加上前缀.

Also note that .replace()is notjQuery code, it's JavaScript.

另外请注意,.replace()不是jQuery的代码,它的JavaScript。

jQuery's .text()method returns a JavaScript Stringvalue. So anything you do with that string - such as the .replace()call - is actually a JavaScript Stringmethod.

jQuery 的.text()方法返回一个 JavaScriptString值。所以你对这个字符串所做的任何事情——比如.replace()调用——实际上都是一个 JavaScriptString方法。

The distinction is important when you want to research a problem: a search for "javascript string replace" will get you better information than "jquery replace".

当您想研究问题时,区别很重要:搜索“javascript string replace”将比“jquery replace”获得更好的信息。

回答by pvnarula

You need to escape the "."

您需要转义“。”

test.text().replace(/\./g, "");

回答by ayyp

It has to be var r = test.text().replace(/\./g, "");instead of var r = test.text().replace(/./g, "");because you need to escape the .in order for it to be replaced.

它必须是var r = test.text().replace(/\./g, "");而不是var r = test.text().replace(/./g, "");因为您需要转义.才能替换它。

回答by Mirko Cianfarani

http://jsfiddle.net/mrk1989/nJZMf/4/

http://jsfiddle.net/mrk1989/nJZMf/4/

Solution because I add \in var r = test.text().replace(/\./g, "");

解决办法,因为我加\var r = test.text().replace(/\./g, "");

回答by Kamil Witkowski

The problem was that you did not escape dot.

问题是你没有转义点。

But keep in mind that:

但请记住:

.replace(".", "");

.replace(".", "");

.replace(/\./g, "");

.replace(/\./g, "");

are two different things.

是两个不同的东西。

For example: https://jsfiddle.net/rmhpkz9n/1/

例如:https: //jsfiddle.net/rmhpkz9n/1/