I want to surround each of the following array items with the ^
sign like this:
const signs = [',', '!!', '?!', '!?', '...', '..', '.', '?', '؟!', '!؟', '!', '؟', ':'];
const input = 'this is, a text?'
// this is the desired result : 'this is ^,^ a text ^?^'
as you see if there is no space between the item and the word next to it a space should be added too.
I used a complex function to do this but I think this could be done easily with regex too.
How would you do this ?
here is the function (which still is in development ):
function modifySelectedPortion(e) {
const translateSigns = translationInput.value.split(' ').filter(s => acceptedSigns.includes(s));
if(translateSigns.length) {
console.log(translateSigns);
}
const selectionEnd = translationInput.selectionEnd;
const selectionStart = translationInput.selectionStart;
const color = e.target.dataset.color;
if(!color) return;
const signs = { blue: '|', red: '$', orange: '^', purple: '#', green: '~', quote: '@', braket: 'braket', pranthesis: 'pranthesis' };
if(color == 'none') return reset();
if(color == 'copy') return copy();
const signA = color == 'bracket' ? '[ ' : color == 'pranthesis' ? '( ' : signs[color];
const signB = color == 'bracket' ? ' ]' : color == 'pranthesis' ? ' )' : signs[color];
const point = translationInput.value[selectionEnd - 1];
const increase = ['bracket', 'pranthesis'].includes(color) ? 2 : 1;
// const start = translationInput.selectionStart;
const finish = point == ' ' ? selectionEnd increase - 1: selectionEnd increase;
const textStart = translationInput.value;
translationInput.value = textStart.substring(0, selectionStart) signA textStart.substring(selectionStart);
const textFinish = translationInput.value;
translationInput.value = textFinish.substring(0, finish) signB textFinish.substring(finish);
translationInputPreview();
function reset() {
for(let key of Object.keys(signs)) {
translationInput.value = translationInput.value.replaceAll(signs[key], '');
}
translationInputPreview();
}
function copy() {
translationInput.value = getCookie('translate-input') || '';
translationInputPreview();
}
}
CodePudding user response:
I would go with regex
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)
CodePudding user response:
I think we could use Regex in this issue
const input = 'this is, a text? are you happy !! haha';
const regex = /(!!|,|\?\?)/g
console.log(input.replaceAll(regex, "^$1^"))
// output: this is^,^ a text? are you happy ^!!^ haha
I created a group (!!|,|\?\?)
, it means find characters that matches !!
, ,
or ??
. And the |
means OR
So, you can put your [',', '!!', '?!', '!?', '...', '..', '.', '?', '؟!', '!؟', '!', '؟', ':']
inside the brackets and split them with |
to resolve the issue
Hope it helpful !
CodePudding user response:
Create a regex that matches any of the elements in your signs
array.
const pattern = /,|\?/
I only added ',' and '?' to the pattern, you can add the rest, just be careful to escape the special characters properly like I did with the ?
.
Then you can do
"this is, a text?".replace(pattern, x => '^' x '^')
Which will output 'this is^,^ a text^?^'