正则表达式从javascript中的匹配字符串中获取下一行值?

时间:2016-01-08 18:13:04

标签: javascript regex-negation regex-lookarounds

function KeywordSearchRegards(string, regex1) 
{   
    while (Regards = regex1.exec(string)) 
    {
        //matchedValues.push(match[index]);
        matchedValuesRegards.push(Regards);
    }   
} 

var RegexRegard=/\s?Regards,\s?(.+)?/g;
var matchedValuesRegards=[];
var Regards ;

String1是:

  

的问候,
   XYZ

1 个答案:

答案 0 :(得分:0)

var letter = document.querySelector('#letter').innerHTML,
    
    /*
      break down of the RegEx
      
      (?:regards|sincerely)  the word regards or sincerely (non capture group), 
      .*                     a comma if its there 
      \n+                    one or many new lines
      (.*)                   capture the content to the next new line
      
      note:
      
      you could capture the rest of the document with
      ([\s\S]+)
    */
    
    match = letter.match(/(?:regards|sincereley),*\n+(.*)/im);
    matchRest = letter.match(/(?:regards|sincereley),*\n+([\s|\S]+)/im)

console.log( match[1] );
console.log( matchRest[1]);
<script src="http://codepen.io/synthet1c/pen/WrQapG.js"></script>
<pre id="letter">
Hello Buddy,

blah blah blah more cowebell.

Regards,

Christopher Walkin
www.christopher-walkin.com
69 somewhere street
</pre>