javascript删除少于3个字符的单词
我厌倦了删除所有少于 3 个字符的单词,例如 in, on ,the...
。
我的代码不适合我,Uncaught TypeError: Object ... has no method 'replace'
寻求帮助。
var str = 'Proin néc turpis eget dolor dictǔm lacínia. Nullam nǔnc magna, tincidunt eǔ porta in, faucibus sèd magna. Suspendisse laoreet ornare ullamcorper. Nulla in tortòr nibh. Pellentesque sèd est vitae odio vestibulum aliquet in nec leo.';
var newstr = str.split(" ").replace(/(\b(\w{1,3})\b(\s|$))/g,'');
alert(newstr);
I am tired to remove all the words less than 3 characters, like in, on ,the...
.
My code not work for me, Uncaught TypeError: Object ... has no method 'replace'
ask for a help.
var str = 'Proin néc turpis eget dolor dictǔm lacínia. Nullam nǔnc magna, tincidunt eǔ porta in, faucibus sèd magna. Suspendisse laoreet ornare ullamcorper. Nulla in tortòr nibh. Pellentesque sèd est vitae odio vestibulum aliquet in nec leo.';
var newstr = str.split(" ").replace(/(\b(\w{1,3})\b(\s|$))/g,'');
alert(newstr);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您需要更改
split
和replace
的顺序:否则,您最终会在数组上调用
replace
,而数组没有此方法。查看实际操作。
注意:您当前的正则表达式无法正确处理“短”单词后紧跟标点符号的情况。您可以稍微更改它来做到这一点:
除此之外,您还必须注意结果数组可能包含空字符串这一事实(因为删除由空格分隔的连续短单词最终会在字符串中留下连续的空格)分裂)。因此,您可能还想更改
拆分
的方式。所有这些都为我们提供了:查看实际操作。
更新: 正如 Ray Toal 在评论中正确指出的那样,在 JavaScript 正则表达式中
\w
确实不匹配非 ASCII 字符(例如带有重音符号的字符) )。这意味着上述正则表达式将无法正常工作(它们将在某些其他风格的正则表达式上正常工作)。不幸的是,没有方便的方法解决这个问题,您必须将\w
替换为字符组,例如[a-zA-Zéǔí]
,并执行相反的操作<代码>\W。更新:
呃,在 JavaScript 正则表达式中做到这一点并不容易。我想出了这个正则表达式:
...我仍然不喜欢它,因为我必须手动在其中包含
ǔ
。查看实际操作。
You need to change the order of
split
andreplace
:Otherwise, you end up calling
replace
on an array, which does not have this method.See it in action.
Note: Your current regex does not correctly handle the case where a "short" word is immediately followed by a punctuation character. You can change it slightly to do that:
Apart from that, you also have to take care of the fact that the resulting array may contain empty strings (because deleting consecutive short words separated by spaces will end up leaving consecutive spaces in the string before it's split). So you might also want to change how you
split
. All of this gives us:See it in action.
Update: As Ray Toal correctly points out in a comment, in JavaScript regexes
\w
does not match non-ASCII characters (e.g. characters with accents). This means that the above regexes will not work correctly (they will work correctly on certain other flavors of regex). Unfortunately, there is no convenient way around that and you will have to replace\w
with a character group such as[a-zA-Zéǔí]
, and do the converse for\W
.Update:
Ugh, doing this in JavaScript regex is not easy. I came up with this regex:
...which I still don't like because I had to manually include the
ǔ
in there.See it in action.
试试这个:
现场演示: http://jsfiddle.net/sTfEs/1/< /a>
Try this:
Live demo: http://jsfiddle.net/sTfEs/1/
str.split(" ")
返回一个数组,该数组没有替换方法。其次,您可能不会为此使用正则表达式。 JavaScript 对正则表达式中的非 ASCII 字母没有很好的支持。请参阅匹配非英语字符的正则表达式?。如果您需要使用正则表达式,那里有提示。
顺便说一句,在所有正则表达式风格中,
\w{1,3}
DOES NOT 匹配"néc"
正如您可能知道的,\w
是[A-Za-z_]
。有关示例,请参阅 http://jsfiddle.net/3YWSC/。您是否只想匹配非空格的单词?或者您只想查找三个或更少字母的单词?一方面,您跨空格分割,但另一方面,您使用了
\w
。我会同意丹尼斯的回答。str.split(" ")
returns an array, which does not have a replace method.Secondly, you probably don't use regexes for this. JavaScript does not have good support for non-ASCII letters in regexes. See Regular expression to match non-English characters?. If you need to use a regex, there are hints in there.
And BTW, in all regex flavors,
\w{1,3}
DOES NOT match"néc"
As you probably know,\w
is[A-Za-z_]
. See http://jsfiddle.net/3YWSC/ for an example.Are you only trying to match words of non-spaces? Or are you looking to for words of three or less letters only? On the one hand you split across spaces, but on the other you used
\w
. I would go with something like Dennis's answer.尝试
Try
使用少于 20 个字符的 lodash:
Using lodash with less then 20 chars:
使用过滤方法
Using The filter method