正如标题所示:
我正在尝试将句子拆分为逗号分隔的字符串或数组,其中包含长度大于 2 个字符且唯一的已清理单词(已删除重复项)。
示例字符串可能是:
$sString = 'Stackoverflow's users are awesome!!! Stackoverflow, is the "best" technical questions and answers website on the interwebnet!';
文章完成:
$sStringAfterProcessing = 'stackoverflow, users, are, awesome, the, best, technical, questions, and, answers, website, interwebnet';
请注意,第一个堆栈流已删除 ,标点符号和重复项均已删除。
这看起来可能会变得非常复杂。
欢迎提出建议,非常感谢所有帮助。
最佳答案
这里...
str = str.replace(/[^\w\s]/ig, "").replace(/\s/g, ", ");
将产生:
Stackoverflows, users, are, awesome, Stackoverflow, is, the, best, technical, questions, and, answers, website, on, the, interwebnet
示例: http://jsfiddle.net/ktFj2/1/
或者,采用数组格式:
var arr = str.replace(/[^\w\s]/ig, "").split(" ");
示例: http://jsfiddle.net/nnKV8/
更新:要从数组中删除重复项(以及长度为 < 2
的项目),如下所示:
var cleaned = [];
for(var i = 0; i < arr.length; i++) {
var el = arr[i];
if (el.length > 2 && $.inArray(el, cleaned) < 0) {
cleaned.push(el);
}
}
关于javascript - 使用 jQuery 将复杂的标点字符串拆分为大于 2 个字符的常规单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7732762/