【发布时间】:2010-05-12 09:52:09
【问题描述】:
var str = 'single words "fixed string of words"';
var astr = str.split(" "); // need fix
我希望数组是这样的:
var astr = ["single", "words", "fixed string of words"];
【问题讨论】:
标签: javascript regex split
var str = 'single words "fixed string of words"';
var astr = str.split(" "); // need fix
我希望数组是这样的:
var astr = ["single", "words", "fixed string of words"];
【问题讨论】:
标签: javascript regex split
接受的答案并不完全正确。它在非空格字符上分隔,例如 .和 - 并在结果中留下引号。这样做以排除引号的更好方法是捕获组,如下所示:
//The parenthesis in the regex creates a captured group within the quotes
var myRegexp = /[^\s"]+|"([^"]*)"/gi;
var myString = 'single words "fixed string of words"';
var myArray = [];
do {
//Each call to exec returns the next regex match as an array
var match = myRegexp.exec(myString);
if (match != null)
{
//Index 1 in the array is the captured group if it exists
//Index 0 is the matched text, which we use if no captured group exists
myArray.push(match[1] ? match[1] : match[0]);
}
} while (match != null);
myArray 现在将包含 OP 要求的内容:
single,words,fixed string of words
【讨论】:
'apple banana "nova scotia" "british columbia"' >> "apple", "banana", "nova scotia", "british columbia" - 我学到了一些新的即即 JavaScript!:-)
str.match(/\w+|"[^"]+"/g)
//single, words, "fixed string of words"
【讨论】:
str.match(/\S+|"[^"]+"/g)
'single words "fixed string of \"quoted\" words"' 即使有了 Awalias 的更正,这给出了:["single", "words", ""fixed", "string", ""of", "words""] 您需要处理转义的引号,但不要绊倒并抓住和转义的反斜杠。我认为它最终会变得比你真正想用正则表达式处理的更复杂。
这使用了拆分和正则表达式匹配的混合。
var str = 'single words "fixed string of words"';
var matches = /".+?"/.exec(str);
str = str.replace(/".+?"/, "").replace(/^\s+|\s+$/g, "");
var astr = str.split(" ");
if (matches) {
for (var i = 0; i < matches.length; i++) {
astr.push(matches[i].replace(/"/g, ""));
}
}
这将返回预期的结果,尽管单个正则表达式应该能够完成所有操作。
// ["single", "words", "fixed string of words"]
更新 这是S.Mark提出的方法的改进版
var str = 'single words "fixed string of words"';
var aStr = str.match(/\w+|"[^"]+"/g), i = aStr.length;
while(i--){
aStr[i] = aStr[i].replace(/"/g,"");
}
// ["single", "words", "fixed string of words"]
【讨论】:
这可能是一个完整的解决方案: https://github.com/elgs/splitargs
【讨论】:
ES6 方案支持:
代码:
str.match(/\\?.|^$/g).reduce((p, c) => {
if(c === '"'){
p.quote ^= 1;
}else if(!p.quote && c === ' '){
p.a.push('');
}else{
p.a[p.a.length-1] += c.replace(/\\(.)/,"$1");
}
return p;
}, {a: ['']}).a
输出:
[ 'single', 'words', 'fixed string of words' ]
【讨论】:
这会将其拆分为一个数组,并从任何剩余的字符串中去除周围的引号。
const parseWords = (words = '') =>
(words.match(/[^\s"]+|"([^"]*)"/gi) || []).map((word) =>
word.replace(/^"(.+(?="$))"$/, '$1'))
【讨论】:
此解决方案适用于双引号 (") 和单引号 ('):
代码:
str.match(/[^\s"']+|"([^"]*)"/gmi)
// ["single", "words", "fixed string of words"]
这里显示了这个正则表达式的工作原理:https://regex101.com/r/qa3KxQ/2
【讨论】:
直到我找到@dallin 的答案(此线程:https://stackoverflow.com/a/18647776/1904943)我在通过 JavaScript 处理混合了未引用和引用的术语/短语的字符串时遇到了困难。
在研究这个问题时,我进行了许多测试。
由于我发现很难找到这些信息,我整理了相关信息(如下),这可能对其他寻求在 JavaScript 中处理包含引号的单词的字符串的答案的人有用。
let q = 'apple banana "nova scotia" "british columbia"';
提取[仅]引用的单词和短语:
// https://stackoverflow.com/questions/12367126/how-can-i-get-a-substring-located-between-2-quotes
const r = q.match(/"([^']+)"/g);
console.log('r:', r)
// r: Array [ "\"nova scotia\" \"british columbia\"" ]
console.log('r:', r.toString())
// r: "nova scotia" "british columbia"
// ----------------------------------------
// [alternate regex] https://www.regextester.com/97161
const s = q.match(/"(.*?)"/g);
console.log('s:', s)
// s: Array [ "\"nova scotia\"", "\"british columbia\"" ]
console.log('s:', s.toString())
// s: "nova scotia","british columbia"
提取 [all] 未引用、引用的单词和短语:
// https://stackoverflow.com/questions/2817646/javascript-split-string-on-space-or-on-quotes-to-array
const t = q.match(/\w+|"[^"]+"/g);
console.log('t:', t)
// t: Array(4) [ "apple", "banana", "\"nova scotia\"", "\"british columbia\"" ]
console.log('t:', t.toString())
// t: apple,banana,"nova scotia","british columbia"
// ----------------------------------------------------------------------------
// https://stackoverflow.com/questions/2817646/javascript-split-string-on-space-or-on-quotes-to-array
// [@dallon 's answer (this thread)] https://stackoverflow.com/a/18647776/1904943
var myRegexp = /[^\s"]+|"([^"]*)"/gi;
var myArray = [];
do {
/* Each call to exec returns the next regex match as an array. */
var match = myRegexp.exec(q); // << "q" = my query (string)
if (match != null)
{
/* Index 1 in the array is the captured group if it exists.
* Index 0 is the matched text, which we use if no captured group exists. */
myArray.push(match[1] ? match[1] : match[0]);
}
} while (match != null);
console.log('myArray:', myArray, '| type:', typeof(myArray))
// myArray: Array(4) [ "apple", "banana", "nova scotia", "british columbia" ] | type: object
console.log(myArray.toString())
// apple,banana,nova scotia,british columbia
使用集合(而不是数组):
// https://stackoverflow.com/questions/28965112/javascript-array-to-set
var mySet = new Set(myArray);
console.log('mySet:', mySet, '| type:', typeof(mySet))
// mySet: Set(4) [ "apple", "banana", "nova scotia", "british columbia" ] | type: object
迭代集合元素:
mySet.forEach(x => console.log(x));
/* apple
* banana
* nova scotia
* british columbia
*/
// https://stackoverflow.com/questions/16401216/iterate-over-set-elements
myArrayFromSet = Array.from(mySet);
for (let i=0; i < myArrayFromSet.length; i++) {
console.log(i + ':', myArrayFromSet[i])
}
/*
0: apple
1: banana
2: nova scotia
3: british columbia
*/
旁白
上面的 JavaScript 响应来自 FireFox 开发者工具(F12,来自网页)。我创建了一个空白 HTML 文件,该文件调用了我用 Vim 编辑的 .js 文件,作为我的 IDE。 Simple JavaScript IDE
根据我的测试,克隆集似乎是深层副本。 Shallow-clone an ES6 Map or Set
【讨论】:
我也注意到消失的字符。我认为您可以包含它们 - 例如,要让它在单词中包含“+”,请使用“[\w\+]”之类的东西,而不仅仅是“\w”。
【讨论】: