Anna F
Anna F

Reputation: 1683

How to replace certain symbols from the whole text except quoted text with REGEX

I have a variable text like this:

"Lorem ipsum dolor sit amet; consetetur sadipscing elitr; sed diam nonumy eirmod tempor "invidunt; ut labore" et dolore magna aliquyam erat/ sed diam voluptua."

What I would like to do is replace all symbols from an array:

var symbolsToreplace = [';', '/', '.']

to a comma. Also What I would like to do is NOT to replace anything within quotes, so all symbols stay the same. Here is Regex to detect quotes:

var detectQuotes = /"([^"]*)"/g;

For now I have achieved one part of task (here I replace all the symbols from an array to ','):

  symbolsToreplace.map(function (s) {
    if(text.indexOf(s) !== -1 ) {
      text.replace(s, ',');
    }
  })

How could I apply the Regex detectQuotes, so thre symbols inside the quotes will be ignored?

Upvotes: 2

Views: 70

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626903

You may use a replace method with the regex that will capture the substring between double quotes and only match the chars you want to replace, then check inside the anonymous method if Group 1 matched, and if yes, restore the match, else, replace it with the comma:

var s = 'Lorem ipsum dolor sit amet; consetetur sadipscing elitr; sed diam nonumy eirmod tempor "invidunt; ut labore" et dolore magna aliquyam erat/ sed diam voluptua.';
console.log(
    s.replace(/"([^"]+)"|[;.\/]/g, function($0,$1) { return $1 ? $0 : ',';})
    // ES6:
    //s.replace(/"([^"]+)"|[;.\/]/g, ($0,$1) => $1 ? $0 : ',')
);

Details

  • "([^"]+)" - matches a ", then captures into Group 1 any one or more chars other than " and then matches a "
  • | - or
  • [;.\/] - a ;, . or / char
  • function($0,$1) { return $1 ? $0 : ',';} - assign the whole match to $0 and the captured substring to $1. If $1 is not empty, replace with the whole match, else, replace with ,.

Upvotes: 0

Alexander
Alexander

Reputation: 4527

You could split a text by a double quote, then apply replacement only for odd fragments, and then concatenate results:

var text = "Lorem ipsum dolor sit amet; consetetur sadipscing elitr; sed diam nonumy eirmod tempor \"invidunt; ut labore\" et dolore magna aliquyam erat/ sed diam voluptua.";
var delimiters = ';./',
  template = new RegExp('[' + delimiters + ']','g')

var replaced = text
  .split('"')
  .reduce(function(previous, current, index) {
    current = (index % 2 === 0) ? current.replace(template, ',') : current;
    return previous + '"' + current;
  }, '');

console.log(replaced);

Upvotes: 2

Related Questions