Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Equivalent of PHP's regex for punctuation

I'm using the following line of PHP to remove punctuation from strings:

$key = preg_replace("/\p{P}/u", "", $key);

Does anyone know how to do the same thing in Javascript/jQuery?

I know you can use jQuery's replace() like PHP's preg_replace(). I just don't know what regular expression to use.

like image 621
liz Avatar asked Feb 15 '26 13:02

liz


1 Answers

Something like this :

<script type="text/javascript">
  var str = "Some text here ...";
  var pattern = /\p{P}/u;
  document.write(str.replace(pattern,''));
</script>

Edit :

It seems that Javascript is not PECL compatible, so the p{P} will not work.

like image 84
Oussama Jilal Avatar answered Feb 17 '26 03:02

Oussama Jilal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!