最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript Replace Function - Stack Overflow

programmeradmin2浏览0评论

I am working with a javascript function and I just got a liitle problem. I want the function to replace the "enters" to "#" so I use:ent = ent.replace(/\\n/gi,"*#*"); and it works perfect when the ent do have any "enter" but when the ment does not have any "enter", or the ment is blank, It stops working and sends an error which says:

'NoneType' object has no attribute 'replace'

How can I control that if it has any "enter" it replaces, if not, does not do anything

the "ent" es from this:

<textarea id="ent"></textarea>

Thank You!

I am working with a javascript function and I just got a liitle problem. I want the function to replace the "enters" to "#" so I use:ent = ent.replace(/\\n/gi,"*#*"); and it works perfect when the ent do have any "enter" but when the ment does not have any "enter", or the ment is blank, It stops working and sends an error which says:

'NoneType' object has no attribute 'replace'

How can I control that if it has any "enter" it replaces, if not, does not do anything

the "ent" es from this:

<textarea id="ent"></textarea>

Thank You!

Share Improve this question edited Jan 30, 2012 at 17:00 mauguerra asked Jan 30, 2012 at 16:52 mauguerramauguerra 3,8585 gold badges33 silver badges37 bronze badges 1
  • Where does ent e from? Can you reproduce this in a jsfiddle? – James M Commented Jan 30, 2012 at 16:53
Add a ment  | 

3 Answers 3

Reset to default 4

The statement

ent = ent.replace(/\\n/gi,"*#*");

...says "replace all parts of the string that are a backslash followed by the letter n with # (case insensitive). If your goal is to replace newlines, you have one too many backslash:

ent = ent.replace(/\n/gi,"*#*");

That alone won't be the problem, though. From the error message, it sounds as though sometimes ent isn't a string, and thus has no replace function. Probably it's undefined or null. If you know it'll be undefined, null, or a string, you can just do this:

if (ent) {
    ent = ent.replace(/\n/gi,"*#*");
}

If it may be a number or something, and regardless you always want a string in the end, then:

ent = (ent === undefined || ent === null)
         ? ""
         : String(ent).replace(/\n/gi,"*#*");

(Don't just do ent = ent ? ... because if ent is the number 0, that won't give you the expected result.)


BTW, since some browsers use \r\n for newlines and others use \n, you're probably better off with \r?\n (or \r{0,1}\n) rather than just \n in the regex.

The problem depends upon what is actual in ent. If ent has a DOM element in it, then you need to get the value from the input field so it has a string:

ent.value = ent.value.replace(/\n/g,"*#*");

If, the issue is that ent is sometimes null or undefined, I would suggest checking first to see if ent has a valid string in it:

if (ent) {
    ent = ent.replace(/\n/g,"*#*");
}

For the null or undefined case, you could also just precondition ent like this:

ent = ent || "";
ent = ent.replace(/\n/g,"*#*");

You may note that I changed a couple things in the replace function too. If you're just trying to replace the newline character, you don't need the double backslash inside a regex definition and there's no reason for the "i" flag on the regex since you don't have any letters in the pattern.

To know specifically which of these applies, you would need to show us the code that gets the value into ent so we can see what it actually has in it or you need to examine ent in the debugger or console.log to see what it has in it.

This sounds like a "blank" ment is not a string, but something like null instead, so it doesn't have a replace function.

Try

ent= (ment && ent.length>0)?ent.replace(/\\n/gi,"*#*"):ent;
发布评论

评论列表(0)

  1. 暂无评论