简体   繁体   English

带正则表达式的String.replace()

[英]String.replace() with regular expression

I'm having difficulty getting String.replace() to work with JavaScript. 我很难让String.replace()与JavaScript一起使用。 It returns in console TypeError: url is undefined 它在控制台TypeError中返回:url未定义

var url = $(this).attr("href");
var id  = url.replace(/(^.{4})(&remove=1)/, "");
console.log(id);

The jquery statement $(this).attr("href") is returning undefined. jQuery语句$(this).attr(“ href”)返回未定义。

Thats why it says url is undefined. 这就是为什么说url未定义的原因。

If your error is TypeError: url is undefined, it means that "$(this).attr("href");" 如果您的错误是TypeError:URL未定义,则表示“ $(this).attr(“ href”);“ returns undefined. 返回未定义。 Your regular expression is ok. 您的正则表达式还可以。

Try outputin the content of this, $(this) and $(this).attr("href"). 尝试输出this($(this)和$(this).attr(“ href”))的内容。 You'll see where the problem is (your whole selector may be broken, or you may select an element that just does not have the "href" attribute). 您将看到问题出在哪里(您的整个选择器可能已损坏,或者您选择的元素只是没有“ href”属性)。

It appears that either this is not a DOM object or if it is a DOM object, then it doesn't have an href attribute. 看来this不是DOM对象,或者如果它是DOM对象,则它没有href属性。 Here's the logic and simple test app that leads to that conclusion. 这是得出结论的逻辑和简单测试应用程序。

Your error means that url is undefined. 您的错误意味着url未定义。 Since url is the product of $(this).attr("href") , we have to figure out how that expression can be undefined. 由于url$(this).attr("href")的乘积,因此我们必须弄清楚该表达式如何未定义。

As long as you have jQuery installed, then $(this) can't be undefined and if it was, you'be getting a different error about referencing the .attr() method. 只要安装了jQuery,就不能取消定义$(this) ,如果是$$ this,则在引用.attr()方法时会遇到另一个错误。 So, we can assume that $(this) must be a valid jQuery object. 因此,我们可以假设$(this)必须是有效的jQuery对象。

So, it appears that you either have an empty jQuery object or that the this element does not have an href attribute. 因此,看来您要么有一个空的jQuery对象,要么this元素没有href属性。

Running a quick jsFiddle test shows that either condition will result in url being undefined. 运行快速的jsFiddle测试表明,任何一种情况都会导致url未定义。 So, you have to figure out whether this is not a DOM object or whether your DOM object doesn't' have the href attribute. 因此,您必须确定this是否不是DOM对象,或者您的DOM对象是否没有href属性。 It's one of those two causes. 这是这两个原因之一。

Here's the test code: 这是测试代码:

// all  of these tests print "undefined" to the console

// a DOM object that doesn't have the href attribute because
// #test is just an ordinary div
var url = $("#test").attr("href");
console.log(url);

// an empty jQuery object because #foo doesn't exist
url = $("#foo").attr("href");
console.log(url);

// a non DOM object in the jQuery object
url = $({}).attr("href");
console.log(url);

You can debug this more yourself by doing this: 您可以通过以下操作自己进行更多调试:

console.log("begin marker");
console.log(this);
console.log(this.href);
var url = $(this).attr("href")
console.log(url);
console.log("end marker");

And, then report back what those console.log() statements show. 然后,报告那些console.log()语句显示的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM