#regex #replace
#регулярное выражение #заменить
Вопрос:
Я сканирую QR-код, и мне нужен скрипт для замены запятых на a ( t)
Мои результаты таковы:
820-20171-002, Ноябрь 24, 2020,,,13,283.40,, Майк Шмоу
Моя проблема в том, что я не хочу ставить запятую после даты. Прямо сейчас у меня есть следующее — которое действительно работает для замены запятых табуляцией.
decodeResults[0].содержимое.заменить(/,/g, «t»);
Я пытаюсь заменить /,/g выражением, чтобы заменить все запятые, за исключением 3-го вхождения.
Ответ №1:
Используйте
.replace(/(?<!b[a-zA-Z]{3}s d{1,2}(?=,s*d{4})),/g, 't')
Смотрите Доказательство
Объяснение
--------------------------------------------------------------------------------
(?<! Negative lookbehind start, fail if pattern matches
--------------------------------------------------------------------------------
b the boundary between a word char (w)
and something that is not a word char
--------------------------------------------------------------------------------
[a-zA-Z]{3} any character of: 'a' to 'z', 'A' to 'Z'
(3 times)
--------------------------------------------------------------------------------
s whitespace (n, r, t, f, and " ") (1
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
d{1,2} digits (0-9) (between 1 and 2 times
(matching the most amount possible))
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
, ','
--------------------------------------------------------------------------------
s* whitespace (n, r, t, f, and " ")
(0 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
d{4} digits (0-9) (4 times)
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
) end of negative lookbehind
--------------------------------------------------------------------------------
, ','