Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace all BUT the first occurrence of a pattern in string

quick question: my pattern is an svg string and it looks like l 5 0 l 0 10 l -5 0 l 0 -10 To do some unittest comparison against a reference I need to ditch all but the first l I know i can ditch them all and put an 'l' upfront, or I can use substrings. But I'm wondering is there a javascript regexp idiom for this?

like image 995
dr jerry Avatar asked Oct 31 '11 21:10

dr jerry


People also ask

How do you replace the first occurrence of a character in a string?

Use the replace() method to replace the first occurrence of a character in a string. The method takes a regular expression and a replacement string as parameters and returns a new string with one or more matches replaced.

How do you replace all occurrences of a regex pattern in a string?

count : Maximum number of pattern occurrences to be replaced. The count must always be a positive integer if specified. . By default, the count is set to zero, which means the re. sub() method will replace all pattern occurrences in the target string.

How do you replace a pattern in a string?

replace() The replace() method returns a new string with one, some, or all matches of a pattern replaced by a replacement . The pattern can be a string or a RegExp , and the replacement can be a string or a function called for each match. If pattern is a string, only the first occurrence will be replaced.

Does string replace replacing all occurrences?

The String type provides you with the replace() and replaceAll() methods that allow you to replace all occurrences of a substring in a string and return the new version of the string.


1 Answers

You can try a negative lookahead, avoiding the start of the string:

/(?!^)l/g 

See if online: jsfiddle

like image 161
Mark Byers Avatar answered Sep 21 '22 10:09

Mark Byers