i have this string "می آییم و می رویم". and i want to replace space after "mi" (می) with Zero-width space.
here is the regex which work's fine in PHP:
preg_replace("#((\s\x{0645}\x{06CC})+( )+([\x{0600}-\x{06EF}]{1,}){1,})#u", "$2\xE2\x80\x8C$4", $value, 1);
and here is the regex for javascript which does not work:
pattern = /((\s\\x{0645}\\x{06CC})+( )+([\\x{0600}\-\\x{06EF}]{1,}){1,})/g;
value = value.replace( new RegExp(pattern), "$2\xE2\x80\x8C$4" );
In JavaScript you need to use \u0abc instead of \x{0abc} for Unicode characters. I also refactored the use of the PHP look back parameters ($2 and $4) as they do not exist as such in JavaScript.
var str = `می آییم و می رویم`
var regex = new RegExp(/(\s\u0645\u06CC)+( )+([\u0600-\u06EF]{1,}){1,}/g)
matches = regex.exec(str);
document.write(str.replace(matches[1]+' '+matches[3],matches[1]+'\u200C'+matches[3]))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With