JustFun2020
JustFun2020

Reputation: 131

Replace string if specific characters / string are included at the beginning and end

Background: the init string is always different (but it contains aa== at the beginning and ==b at the end)

example:

var string = 'aa==1234^&==b'
var string = 'aa==hello==b'
var string = 'aa==world!==b'

How to replace the string in the middle part? ? ?

string.replace(????, 'TestOK')

result:

var string = 'aa==TestOK==b'
var string = 'aa==TestOK==b'
var string = 'aa==TestOK==b'

Upvotes: 0

Views: 49

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 370689

Replacing everything between the ==s should do it:

const transform = str => str.replace(/(?<===)[^=]+(?===)/g, 'TestOK');
console.log(transform('aa==1234^&==b'));

Upvotes: 2

Related Questions