Thanks to @Konrad we can surround each of the array items with the ^
sign like this:
const signs = [',', '!!', '?!', '!?', '...', '..', '.', '?', '؟!', '!؟', '!', '؟', ':'];
const input = 'this is, a text?'
const str = signs.map(e => e.replace(/\?/g, '\\?').replace(/\./g, '\\.')).join('|')
const regex = new RegExp(` ?(${str}) ?`, 'g')
const result = input.replace(regex, ' ^$1^ ').trim()
console.log(result)
Now the issue is if you have this input as the string:
this is ^,^ a text ^?^ ?
you will get a repetition of ^
sign and we don't want this:
this is ^ ^,^ ^ a text ^ ^?^ ^ ^?^
Here is the desired result actually :
this is ^,^ a text ^?^ ^?^