我需要在某些","上分割如下图案。迹象。
输入:
"stri,ng1 ext,string2 ext,string3, string4 ,string5"
输出:
["stri,ng1 ext", "string2 ext", "string3", "string4", "string5"]
","匹配具有以下规则:
第二种模式的问题是,这还包括" ext"部分。拥有一个有效的模式来匹配","。
会很高兴如果不可能至少有一个短暂的algorythm。有人可以帮忙吗?
我当前不那么漂亮的模式看起来像这样:
\s+(.*?)[^\s+],+\s*|\s*,+\s+|\s+,+\s*
虽然这匹配正确且唯一正确的逗号,但它还包括"分机"部分。我该如何排除它们。
答案 0 :(得分:2)
您可以在Javascript中使用此代码:
var str = 'stri,ng1 ext,string2 ext,string3, string4 ,string5';
var m = str.replace(/ +,|, +| +([^,]*),/g, function($0, $1) {
var p=($1!=undefined)?" "+$1:""; return(p + "##"); }).split('##');
//=> ["stri,ng1 ext", "string2 ext", "string3", "string4", "string5"]
答案 1 :(得分:0)
也想要提供一种无正则表达式的方法。看到这么简单的任务需要多少代码,真是令人印象深刻。
tokenize("stri,ng1 ext,string2 ext,string3, string4 ,string5");
//["stri,ng1 ext", "string2 ext", "string3", "string4 ", "string5"]
function tokenize(str) {
var tokens = [],
i = 0,
tokenStartIndex = 0,
spaceSeenSinceLastToken = false,
nonSpaceSeenSinceLastToken = false,
spacesCountSinceLastNonSpace = 0,
SPACE = ' ',
len = str.length,
nextIndex, char, prevCharIsSpace, nextCharIsSpace, lastToken;
for (; i < len; i++) {
if (SPACE == (char = str[i])) {
spaceSeenSinceLastToken = true;
if (!nonSpaceSeenSinceLastToken) ++tokenStartIndex;
else ++spacesCountSinceLastNonSpace;
continue;
}
if (char != ',') {
spacesCountSinceLastNonSpace = 0;
nonSpaceSeenSinceLastToken = true;
continue;
}
nextIndex = i + 1;
prevCharIsSpace = str[i - 1] == SPACE;
nextCharIsSpace = str[nextIndex] == SPACE;
if (isDirectlyFollowedOrPrecededBySpace() || isFirstCommaPrecededBySpaceAndFollowedByNonSpace()) {
pushToken();
tokenStartIndex = nextIndex;
spaceSeenSinceLastToken = nonSpaceSeenSinceLastToken = false;
spacesCountSinceLastNonSpace = 0;
}
}
pushToken();
return tokens;
function isDirectlyFollowedOrPrecededBySpace() {
return prevCharIsSpace || nextCharIsSpace;
}
function isFirstCommaPrecededBySpaceAndFollowedByNonSpace() {
return spaceSeenSinceLastToken && !nextCharIsSpace;
}
function pushToken() {
var token = str.slice(tokenStartIndex, i - spacesCountSinceLastNonSpace);
token && tokens.push(token);
}
}