Rast Wishes
Rast Wishes

Reputation: 69

Javascript regex only letters and numbers and dash UTF8

Have php regExp

$string = preg_replace("/[^\p{L}|\p{N}]+/u", " ", $string);
$string = str_replace(' ', '-', $string);

Help me to write it on js, need to convert

jbgsbg5%E-th-t65?tw45@%^&*j-y&u-САМОЛЁТ~   ~2><%27;[]~!6456

to

jbgsbg5-E-th-t65-tw45-j-y-u-САМОЛЁТ-2-6456

Thanks!

Upvotes: 0

Views: 887

Answers (3)

Rast Wishes
Rast Wishes

Reputation: 69

only letters, numbers and dash UTF8 JS

link = link.replace(/[^\u00BF-\u1FFF\u2C00-\uD7FF\w]+|[\_]+/ig, '-');

Upvotes: 1

Praveen
Praveen

Reputation: 902

You can try with this regular expression in your javascript :

[\u00BF-\u1FFF\u2C00-\uD7FF\w\-]+

http://regex101.com/r/pE0eL8/1

Upvotes: 0

Taufik Nurrohman
Taufik Nurrohman

Reputation: 3409

You could just remove all non-letters, non-numbers and non-dash with [^a-z0-9\- ... ЛЁ ... ]:

var foo = 'jbgsbg5%E-th-t65?tw45@%^&*j-y&u-САМОЛЁТ~   ~2><%27;[]~!6456';

foo
    .replace(/\s+/g, "") // remove whitespaces
        .replace(/[^a-zA-Z0-9\- ... ЛЁ ... ]/ig, "") // remove non-letters, non-numbers and non-dash characters
            .replace(/\-+/g, '-'); // replace multiple `-` character with single `-`

Where ... ЛЁ ... is for all of your Roman letters.

Upvotes: 0

Related Questions