replace method with Regex - unexpected output logging match $& and Group $1

时间:2017-04-06 16:45:32

标签: javascript regex replace match

I have a regex I intend to use with the .replace method with the intention of extracting paragraphs from a string and pushing each one to an array.

I was struggling with my getValues function and when I logged both Match and Group1 to the console got some unexpected results.

Here's the code wip:

var  mystring = 'Valid prater\nLorem ipsum dolor sit amet, consectetur adipiscing elit. \nProin volutpat facilisis imperdiet. \n Nunc porttito\nMorbi non eros nec arcu condimentum ultrices in ut nunc. \nMaecenas elit tellus, scelerisque ac auctor fermentum, bibendum. '

var paragraphs = [];
var obj = {};

var getValues = function(match,p1) { 
  console.log('Match: ' + match );
  console.log('p1: ' + p1 );

    // obj= {};
    // obj['paragraph'] = p1; 
    // paragraphs.push(obj);
 };

mystring.replace(/([^\\n][^\\]+)/g, getValues);

https://jsfiddle.net/7293mo7y/

Expected output:

Match: Valid prater
p1: Valid prater
Match: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
p1: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Match: Proin volutpat facilisis imperdiet.
p1: Proin volutpat facilisis imperdiet.
Match: Nunc porttito
p1: Nunc porttito
Match: Morbi non eros nec arcu condimentum ultrices in ut nunc.
p1: Morbi non eros nec arcu condimentum ultrices in ut nunc.
Match: Maecenas elit tellus, scelerisque ac auctor fermentum, bibendum. 
p1: Maecenas elit tellus, scelerisque ac auctor fermentum, bibendum.

I'm expecting similar behaviour to this example

Actual output:

Match: Valid prater
Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
Proin volutpat facilisis imperdiet. 
 Nunc porttito
Morbi non eros nec arcu condimentum ultrices in ut nunc. 
Maecenas elit tellus, scelerisque ac auctor fermentum, bibendum. 
p1: Valid prater
Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
Proin volutpat facilisis imperdiet. 
 Nunc porttito
Morbi non eros nec arcu condimentum ultrices in ut nunc. 
Maecenas elit tellus, scelerisque ac auctor fermentum, bibendum. 

Could anyone explain why I'm not getting the expected output when logging match and p1 to the console?

Why is the behaviour different to this example?

What needs to change to get the expected output?

Thanks!

1 个答案:

答案 0 :(得分:1)

You can just take advantage of MULTILINE flag or m in your regex. That allows you to use anchors ^ and $ to match a full line in each match like this:

var  mystring = 'Valid prater\nLorem ipsum dolor sit amet, consectetur adipiscing elit. \nProin volutpat facilisis imperdiet. \n Nunc porttito\nMorbi non eros nec arcu condimentum ultrices in ut nunc. \nMaecenas elit tellus, scelerisque ac auctor fermentum, bibendum. '

var paragraphs = [];
var obj = {};

var getValues = function(match,p1) { 
  console.log('Match: ' + match);
  console.log('p1: ' + p1);
 };

mystring.replace(/^(.*)$/mg, getValues);

Updated JS Fiddle