Oguz Bilgic
Oguz Bilgic

Reputation: 3480

RegExp alphanumeric String + Special Letters

First question : I want to replace all characters other than alphanumeric and Special Letters. For example , somestringğüş iöç123456!@#$%^&*()_+ to somestringğüş iöç123456

Second: For example , some---example--long-string to some-example-long-string

I do not really know regexp , so I need 2 simple regexp strings.Thank you

Upvotes: 1

Views: 694

Answers (2)

kennytm
kennytm

Reputation: 523214

 /* 1. */   return x.replace(/[!@#$%^&*()_+]/g, '');
 /* 2. */   return x.replace(/-{2,}/g, '-');

Upvotes: 3

Markus Jarderot
Markus Jarderot

Reputation: 89171

First. It matches any character that is not alphanumeric, whitespace or non-ascii, and replaced them with the empty string.

str.replace(/[^a-z0-9\s\x80-\uFFFF]+/gi, '');

There are no unicode-classes that I can use, so either I include all unicode characters, or list the ones that are not letters, digits nor whitespace.

Second. It matches any sequence of two or more dashes, and replaces them with a single dash.

str.replace(/-{2,}/g, '-');

Upvotes: 3

Related Questions