mihai
mihai

Reputation: 38573

Regexp matching for some text

I have the following text:

@title1

Some stuff here and some @junk@ here

@newheader

Multiple lines. 
Multiple lines. 
Multiple lines. 

@title3

Extra stuff here.

I need a regexp that would match the text after the titles. First match should return

Some stuff here and some @junk@ here

Also, title is something that starts on a new line with @ and it's followed by some non space characters

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

Upvotes: 1

Views: 133

Answers (4)

Rob W
Rob W

Reputation: 349232

Fiddle: http://jsfiddle.net/u5Khe/

You're looking for this RE: /(?:^|\n)@([^@\n]+)\s*((?:[\S\s](?!\n@))+)/g.

Code:

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"

Explanation of RE:

  • (?:^|\n)@ seeks for the beginning of the title (^@ = "@ at the beginning of a text", \n@ = "@ at the beginning of a new line"
  • ([^@\n]+) means: Match every character except for @ or newline (delimiter of title, as defined by OP)
  • ((?:[\S\s](?!\n@))+) means: Select all + characters \S\s, which are not followed by a newline + @ (?!\n@).
  • /g is the "global" flag = "attempts to get as much matches as possible on a given string"

Your string should be formatted like this:

@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

Upvotes: 1

Narendra Yadala
Narendra Yadala

Reputation: 9664

Try this

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

Upvotes: 0

Kakashi
Kakashi

Reputation: 2195

Try this:

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)

Upvotes: 0

Jason Gennaro
Jason Gennaro

Reputation: 34863

You might be able to do something like this:

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

and then access the third ($3) group.

So...

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);

Example: http://jsfiddle.net/jasongennaro/5Chjf/

fyi... this assumes that @title starts every other line.

Upvotes: 0

Related Questions