Мне нужно заменить все id: от строки до _xid: пробовал, используйте
string.replace (/id:/g, '_xid:')
, но это не работает.
Попробуйте это:
String.prototype.replaceAll = function (search, replacement) { const target = this return target.replace(new RegExp(search, 'g'), replacement) } string.replaceAll('id', '_xid')
let text = 'first id: and second id:' text = text.replace(/id:/g, '_xid:'); console.log(text)
string.replace(/id:/g, '/_xid:/') не может найти ничего, так как символы '/' считаются открытием и закрытием регулярного выражения.
string.replace(/id:/g, '/_xid:/')
Попробуйте
str.replace(/\/id:\//g, '/_xid:/')
, чтобы избежать '\' char.