正则表达式匹配一些文本

时间:2011-09-25 00:32:43

标签: javascript regex

我有以下文字:

@title1

Some stuff here and some @junk@ here

@newheader

Multiple lines. 
Multiple lines. 
Multiple lines. 

@title3

Extra stuff here.

我需要一个与标题后面的文字相匹配的正则表达式。第一场比赛应该返回

  

这里有些东西和@ junk @ here

此外,标题是以@开头的新行开头,后跟一些非空格字符

http://jsfiddle.net/QCNfQ/2/

4 个答案:

答案 0 :(得分:1)

小提琴http://jsfiddle.net/u5Khe/

您正在寻找此RE:/(?:^|\n)@([^@\n]+)\s*((?:[\S\s](?!\n@))+)/g

代码:

var string = "@title1\n\nTest @case@ one\n\n@title2\n\nMulti" +
             "\nline\nstring\n\n@title3\n\nfinal test";
var results = [];

var re = /(?:^|\n)@([^@\n]+)\s*((?:[\S\s](?!\n@))+)/g;
var matches = null;
while((matches = re.exec(string)) != null){
    /* matches[0] = whole block
       matches[1] = title
       matches[2] = body
     */
     var body = matches[2].replace(/\^s+|\s$/g,"");
     results.push(body);
}
//results.length will be 3;
alert(results.join("\n-----------------------\n"));
//Shows an alert with all matches, separated by "\n----------------\n"

RE的解释:

  • (?:^|\n)@寻找标题的开头(^@ =“@在文本的开头”,\n@ =“@在新行的开头”
  • ([^@\n]+)表示:匹配除@或换行符之外的每个字符(标题的分隔符,由OP定义)
  • ((?:[\S\s](?!\n@))+)表示:选择所有+个字符\S\s,后面没有换行符+ @ (?!\n@)
  • /g是“global”flag =“尝试在给定字符串上获得尽可能多的匹配”

您的字符串应格式如下:

@title
Body

@title2
Anything, from @ to @, as long as the next line doesn't start with a @
 @ (There's a whitespace before this @)

@custom title@ Delimited by the @

@Foo
bar

答案 1 :(得分:0)

你可能会做这样的事情:

/(@title\d)(\s)*(.*)/gi;

然后访问第三个($3)组。

因此...

var a = "@title1\n\nSome stuff here and some @junk@";
var a1 = "@title2\n\nExtra stuff here.";

var b = /(@title\d)(\s)*(.*)/gi;
var c = a.replace(b, '$3');
var d = a1.replace(b, '$3');

document.write(c + '<br />' + d);

示例: http://jsfiddle.net/jasongennaro/5Chjf/

fyi ...这假设@title每隔一行开始。

答案 2 :(得分:0)

试试这个:

var text = "@title1

Some stuff here and some @junk@

@title2

Extra stuff here."; 

var output = text.replace(/([^@]+@)(\w+@)/,
            function (all, g1, g2) {
                return [g2, g1].join('');
            }
    );

alert(output)

答案 3 :(得分:0)

试试这个

'\n@title1   test'.match(/(?:\n@title\d)(?:[\s|\n])*(.*)/)[1]