简体   繁体   English

Javascript正则表达式仅字母,数字和破折号UTF8

[英]Javascript regex only letters and numbers and dash UTF8

Have php regExp 有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 帮我在js上写,需要转换

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

to

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

Thanks! 谢谢!

You could just remove all non-letters, non-numbers and non-dash with [^a-z0-9\\- ... ЛЁ ... ] : 您可以使用[^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. ... ЛЁ ...是所有的罗马字母的。

You can try with this regular expression in your javascript : 您可以在javascript中尝试使用此正则表达式:

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

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

仅字母,数字和破折号UTF8 JS

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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