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?
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.
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.
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.
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.
You can try a negative lookahead, avoiding the start of the string:
/(?!^)l/g
See if online: jsfiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With