0

我正在尝试在 AS3 和/或 JavaScript 中编写一个函数,它可以让我获取一个包含在方括号中的单词的字符串并将它们放在一起,同时拆分其余部分。因此,例如,它将采用这样的句子:

这是 [方括号] 中包含 [词组] 的句子的 [示例]。

并将其变成这样的数组:

[This,is,an example,of,a,sentence,that,has,groups of words,enclosed,in,square brackets.]

实现这一目标的最有效方法是什么?我猜它会使用某种正则表达式,可能带有前瞻和/或后视。有任何想法吗?

4

5 回答 5

3

这个正则表达式应该这样做: /[^[][([^]]+)]|(\w+)/g;

它搜索方括号内的所有单词,并在外面创建一个组或所有单词。

示例(javascript):

var testStr="This is [an example] of a sentence that has [groups of words] enclosed in [square brackets.]";

var reSentence = /[^\[]\[([^\]]+)\]|(\w+)/g;
var matchObject;
var result=[];
while(matchObject=reSentence .exec(testStr))
      result.push(matchObject[1] || matchObject[2]);
console.log(result);

javascript 实时示例:http: //jsfiddle.net/UNxB9/

示例(as3):

var testStr:String="This is [an example] of a sentence that has [groups of words] enclosed in [square brackets.]";

var reSentence:RegExp = /[^\[]\[([^\]]+)\]|(\w+)/g;
var matchObject:Object;
var result:Array=[];
while(matchObject=reSentence .exec(testStr))
      result.push(matchObject[1] || matchObject[2]);
trace(result);

as3 现场示例:http ://wonderfl.net/c/2MiG

于 2012-09-06T07:49:09.543 回答
0

只是在这里大声思考......我的第一次尝试是匹配正则表达式,然后用某种独特的符号存储和替换括号(和内容)。然后用空格分割字符串,然后用括号内容替换数组中的符号,顺序与删除的顺序相同。

目前不确定效率,但你必须从某个地方开始。希望这能让事情顺利进行。

于 2012-09-06T07:18:38.833 回答
0

这将做到:

function splitWithBracket(str) {
    var out = str.split(" "), i = 0, begin, end;

    function findNext(chr) {
        while (i < out.length) {
            if (out[i].indexOf(chr) != -1) {
                out[i] = out[i].replace(chr, "");
                return(i);
            }
            i++;
        }
        return(-1);
    }

    do {
        end = -1;
        begin = findNext("[");
        if (begin != -1) {
            end = findNext("]");
            if (end != -1) {
                var tmp = out.splice(begin, end - begin + 1);
                out.splice(begin, 0, tmp.join(" "));
                i = begin + 1;
            }
        }
    } while (end != -1);
    return(out);
}

工作演示:http: //jsfiddle.net/jfriend00/jLNXa/

或使用正则表达式替换/匹配的版本:

function splitWithBracket(str) {
    str = str.replace(/\[([^\]]*)\]/g, function(m, p1) {
            return(p1.replace(/ /g, "!!xx!!"));
    });
    var out = str.split(" ");
    for (var i = 0; i < out.length; i++) {
        out[i] = out[i].replace(/!!xx!!/g, " ")
    }
    return(out);
}

工作演示:http: //jsfiddle.net/jfriend00/Hbjzk/

于 2012-09-06T07:19:07.807 回答
0

试试这个:

var str = 'This is [an example] of a sentence that has [groups of words] enclosed in [square brackets.]';
var finder = /\[?([^\[\]]+)\]?/g;
var result;
while( ( result = finder.exec( str ) ) !== null ){
    console.log( result );
}

祝你好运!

于 2012-09-06T07:24:00.497 回答
0

这将完成工作, http: //jsfiddle.net/mwfHn/2/

    <script>
    myStr = "This is [an example] of a sentence that has [groups of words] enclosed in [square brackets.]";
    myStr = myStr.replace(/\[[a-zA-Z. ]+\]/g, function(match){ return match.replace(/\s+/g,"#").replace(/\[|\]/g,""); }).split(" ").join(",").replace(/#/g," ");
    alert(myStr);
   </script>
于 2012-09-06T07:25:11.870 回答