简体   繁体   中英

Can't replace space with Zero-width space using RegExp

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 \઼ 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\م\ی)+( )+([\؀-\ۯ]{1,}){1,}/g) matches = regex.exec(str); document.write(str.replace(matches[1]+' '+matches[3],matches[1]+'\‌'+matches[3])) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM