简体   繁体   中英

JavaScript Regular Expression: Non-Digit Character

How do I say remove a number preceded by a non-digit and followed by a dash, but leave the preceding non-digit character?

RegExp: /[^\D]4\-/
String: http://localhost/images/4-6-.png
Remove: 4-

The 4- should be removed and it should leave the preceding / or -

This would work: /4\\-/
But it would also remove 14- or 44-

Dynamic Code:

http://jsfiddle.net/flackend/8s9X9/2/

Static Code:

var category_id = 4;
var src         = 'http://localhost/images/4-6-.png';
var regexp      = new RegExp('[^\\D]'+ category_id +'\\-')

$('p').append('regexp: '+ regexp +'<br>');
$('p').append(src +'<br>');

src = src.replace(regexp, '');

$('p').append(src);

You want [\\D] or [^\\d] , but not [^\\D] . Regex is case-sensitive, \\d matches a digit, and \\D matches anything but a digit.

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