javascript - regex to replace 0 in list but not 0 of 10, 20, 30, etc. - using js replace - Stack Overflow

admin2025-04-17  4

I'm trying to create a regex to replace zeros in list with an empty value but not replace the zeros in ten, twenty, thirty, etc.

list = 0,1,0,20,0,0,1,,1,3,10,30,0

desired list = ,1,,20,,,1,,1,3,10,30,

Using this in the javascript replace function

Any help/tips appreciated!

I'm trying to create a regex to replace zeros in list with an empty value but not replace the zeros in ten, twenty, thirty, etc.

list = 0,1,0,20,0,0,1,,1,3,10,30,0

desired list = ,1,,20,,,1,,1,3,10,30,

Using this in the javascript replace function

Any help/tips appreciated!

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked May 1, 2011 at 11:56 shaunwshaunw 3734 silver badges11 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Should be very simple using word boundaries, \b0\b:

s = s.replace(/\b0\b/g, '');

Working example: http://jsbin./ipuru4

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744825117a269065.html

最新回复(0)