Capture and replace first N occurrence of a pattern in a multiline string

priyeshdkr

I have following string from which i expect to replace the first two occurrences of <br> with a \n character.

<br><br><br>. Do not replace <br> here.
1. One
2. Two
3. Three<br><br><br>End of List. Replace first two <br> with \n
New line follows.
<br><br><br>. Do not replace <br> here.

I did write my regex here. I am very new to regex and i am sure this is not an optimized solution. After some tries i was able to select the <br><br> as a capturing group. I want this 3rd capturing group to be my selected match so i can easly replace it with \n. Can someone help me with this?

My expected output is:

<br><br><br>. Do not replace <br> here.
1. One
2. Two
3. Three\n<br>End of List. Replace first two <br> with \n
New line follows.
<br><br><br>. Do not replace <br> here.

var str = `1. One
2. Two
3. Three<br><br><br>End of List
New line follows
`

console.log(
  str.match(/[\n\r].*(\d\.\s+)(?!.*[\n\r](\d\.\s+)).*((<br\s*\/?>){2})/)
);  

Joven28

Try this.

let str = `<br><br><br>. Do not replace <br> here.
1. One
2. Two
3. Three<br><br><br>End of List. Replace first two <br> with \n
New line follows.
<br><br><br>. Do not replace <br> here.`;
console.log(str.replace(/(?<=[0-9]+\..*?)(<br>){2}/g, "\n"));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Replace first occurrence of pattern in a string

Replace the first occurrence of a pattern in a String

Replace first occurrence of a pattern if not preceded with another pattern

Javascript: Replace nth occurrence of substring in multiline string

Replace first occurrence of string in Python

Replace first occurrence of '.' in sql String

swift replace first occurrence of a String

Spark - Replace first occurrence in a string

How to replace first n no. of occurrence of a string using sed and/or awk?

Replace a multiline string with pattern using sed command

replace first occurrence of string after another string

Sed-Replace immediate next string word coming after a particular pattern but only the first occurrence in a file

Fastest way to replace last occurrence of pattern in a string

Keep string up to first occurrence of pattern in R

capture all occurrences of pattern until the first occurrence of a word in Python

How to replace first occurrence of string in Java

How to replace the first occurrence of a character with a string in JavaScript?

Replace first occurrence of substring in a string in SQL

Replace/Remove only the first occurrence found in a string

Replace ONLY the first occurrence of a string in SQL

pattern matching with sub(), unable to catch and replace first occurrence

sed replace first occurrence of "space-tab" pattern

Using Regex to capture content after first occurrence of a string

Replace first occurrence of character in string, but only if first character

get first occurrence of pattern

How to replace n occurrence of a substring in a string in dart?

Is there a method in Java that will replace the first occurrence of a String with a given String?

Oracle Regular Expressions - Replace all occurrence of string matching pattern

Replace first nth occurences of string1 by string2 after string3, within a file (i.e multiline pattern)