Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
728 views
in Technique[技术] by (71.8m points)

regex - 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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...