javascript删除少于3个字符的单词

发布于 2024-12-04 04:54:03 字数 500 浏览 0 评论 0原文

我厌倦了删除所有少于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

挽手叙旧 2024-12-11 04:54:03

您需要更改 splitreplace 的顺序:

var newstr = str.replace(/(\b(\w{1,3})\b(\s|$))/g,'').split(" ");

否则,您最终会在数组上调用 replace,而数组没有此方法。

查看实际操作

注意:您当前的正则表达式无法正确处理“短”单词后紧跟标点符号的情况。您可以稍微更改它来做到这一点:

/(\b(\w{1,3})\b(\W|$))/g
                ^^

除此之外,您还必须注意结果数组可能包含空字符串这一事实(因为删除由空格分隔的连续短单词最终会在字符串中留下连续的空格)分裂)。因此,您可能还想更改拆分的方式。所有这些都为我们提供了:

var newstr = str.replace(/(\b(\w{1,3})\b(\W|$))/g,'').split(/\s+/);

查看实际操作

更新: 正如 Ray Toal 在评论中正确指出的那样,在 JavaScript 正则表达式中 \w 确实匹配非 ASCII 字符(例如带有重音符号的字符) )。这意味着上述正则表达式将无法正常工作(它们将在某些其他风格的正则表达式上正常工作)。不幸的是,没有方便的方法解决这个问题,您必须将 \w 替换为字符组,例如 [a-zA-Zéǔí],并执行相反的操作<代码>\W。

更新:

呃,在 JavaScript 正则表达式中做到这一点并不容易。我想出了这个正则表达式:

([^ǔa-z\u00C0-\u017E]([ǔa-z\u00C0-\u017E]{1,3})(?=[^ǔa-z\u00C0-\u017E]|$))

...我仍然不喜欢它,因为我必须手动在其中包含 ǔ

查看实际操作

You need to change the order of split and replace:

var newstr = str.replace(/(\b(\w{1,3})\b(\s|$))/g,'').split(" ");

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:

/(\b(\w{1,3})\b(\W|$))/g
                ^^

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:

var newstr = str.replace(/(\b(\w{1,3})\b(\W|$))/g,'').split(/\s+/);

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:

([^ǔa-z\u00C0-\u017E]([ǔa-z\u00C0-\u017E]{1,3})(?=[^ǔa-z\u00C0-\u017E]|$))

...which I still don't like because I had to manually include the ǔ in there.

See it in action.

寄人书 2024-12-11 04:54:03

试试这个:

str = str.split( ' ' ).filter(function ( str ) {
    var word = str.match(/(\w+)/);
    return word && word[0].length > 3;
}).join( ' ' );

现场演示: http://jsfiddle.net/sTfEs/1/< /a>

Try this:

str = str.split( ' ' ).filter(function ( str ) {
    var word = str.match(/(\w+)/);
    return word && word[0].length > 3;
}).join( ' ' );

Live demo: http://jsfiddle.net/sTfEs/1/

西瑶 2024-12-11 04:54:03
var words = str.split(" "); //Turns the string into an array of words
var longWords = []; //Initialize array
for(var i = 0; i<words.length; i++){
    if(words[i].length > 3) {
        longWords.push(words[i]);
    }
}
var newString = longWords.join(" "); //Create a new string of the words separated by spaces.
var words = str.split(" "); //Turns the string into an array of words
var longWords = []; //Initialize array
for(var i = 0; i<words.length; i++){
    if(words[i].length > 3) {
        longWords.push(words[i]);
    }
}
var newString = longWords.join(" "); //Create a new string of the words separated by spaces.
对不⑦ 2024-12-11 04:54:03

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.

没有你我更好 2024-12-11 04:54:03

尝试

var newstr = str.replace(/(\b(\w{1,3})\b(\s|$))/g,'').split(" ");

Try

var newstr = str.replace(/(\b(\w{1,3})\b(\s|$))/g,'').split(" ");
夜未央樱花落 2024-12-11 04:54:03

使用少于 20 个字符的 lodash:

let a = ['la','rivière','et','le','lapin','sont','dans','le','près'];

a = _.remove(_.uniq(a),n=>_.size(n)>3); // ['rivière','lapin','sont','dans','près']

Using lodash with less then 20 chars:

let a = ['la','rivière','et','le','lapin','sont','dans','le','près'];

a = _.remove(_.uniq(a),n=>_.size(n)>3); // ['rivière','lapin','sont','dans','près']
飘然心甜 2024-12-11 04:54:03

使用过滤方法

let sentence = "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 .";

let sent = sentence.split(" ").filter((ele) => ele.length > 3).join(" ");

console.log(sent);

Using The filter method

let sentence = "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 .";

let sent = sentence.split(" ").filter((ele) => ele.length > 3).join(" ");

console.log(sent);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文