Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
103
rated 0 times [  110] [ 7]  / answers: 1 / hits: 82049  / 13 Years ago, tue, february 7, 2012, 12:00:00

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);

More From » regex

 Answers
4

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


[#87598] Monday, February 6, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaceyr

Total Points: 510
Total Questions: 97
Total Answers: 116

Location: Solomon Islands
Member since Fri, Oct 8, 2021
3 Years ago
kaceyr questions
;