jquery .replace(/。不要为我工作,而是为别人工作。

[英]jquery .replace(/./g, “”) do not work for me but others


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.

您将通过查看索引的来源找到代码片段。html和搜索.replace。

5 个解决方案

#1


4  

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 r is a jQuery object.

其中r是一个jQuery对象。

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

注意正则表达式在//而不是a中有a。就像你遇到的代码一样。

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 all characters, and you need to prefix it with \ if you want to match . only.

逗号不是正则表达式中的一个特殊字符,因此不需要特殊处理。时间有特殊的意义。正如其他答案所指出的那样,它匹配所有的字符,如果您想要匹配,则需要将其前缀为\。只有。

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

还要注意,.replace()不是jQuery代码,而是JavaScript。

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

方法的作用是:返回一个JavaScript字符串值。因此,任何处理该字符串的操作——例如.replace()调用——实际上都是一个JavaScript字符串方法。

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字符串替换”会比“jquery replace”获得更好的信息。

#2


5  

You need to escape the "."

你需要逃离“。”

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

#3


3  

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”、“);因为你需要逃离。为了让它被取代。

#4


2  

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”、“);

#5


0  

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/。



注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2013/06/06/4172d49601cb3151860ffb6d5f66a28e.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号