Ionut Bejinariu
Ionut Bejinariu

Reputation: 117

How to replace the break with a comma after two words?

I use the following line of code to replace all spaces with commas between words.

SET keyword-virgula EVAL("var spatiu=\"{{Keyword}}\"; spatiu.replace(/ /g,','); ")

How could I make the comma be added after two words?

word word, word word, word word, word word, word word,

Can someone help me? thanks

I am using: Browser: Google Chrome Version 105.0.5195.102 (Official Build) (64-bit)

iMacros Personal Edition License - Addon for Chrome -Version 10.1.1

Windows 10 (64-bit)

Upvotes: 1

Views: 164

Answers (2)

The fourth bird
The fourth bird

Reputation: 163577

You can match 2 times one or more non whitespace characters followed by 1+ whitespace character in between using a pattern.

In the replacement use the full match using $& followed by a comma.

const s = "word word word word word word word word word word";
const regex = /\S+\s+\S+/g
console.log(s.replace(regex, "$&,"));

Upvotes: 1

Sund'er
Sund'er

Reputation: 666

With JS we can achieve , i hv no idea in Regex

var str  = "word word word word word word word word word word word word word word word word word word ";

   var strr = str.split(' ')
   .reduce(function (str, part, i) {
      return str + ((i%2)===0 ? ',' : ' ') + part
   });
console.log(strr);

Upvotes: 0

Related Questions