Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
174
rated 0 times [  180] [ 6]  / answers: 1 / hits: 19065  / 13 Years ago, mon, july 11, 2011, 12:00:00

I'm working with jQuery and am trying to write a pattern replace, but it doesn't work. I have this:



var $featured_rewrite = $('#featured').not('.slideshow');
$featured_rewrite.children().attr('href', $featured_rewrite.find('img').attr('src').replace('/-[0-9]+x[0-9]+./i', '.'));


I don't understand why something like this works:



.replace('-500x277.', '.')


but not this, which I even checked with a tool and made sure it was valid and works:



.replace('/-[0-9]+x[0-9]+./i', '.')

More From » jquery

 Answers
20

'/-[0-9]+x[0-9]+./i' is a string.



/-[0-9]+x[0-9]+./i is regex.



hi.match('/hi/')  // returns null
hi.match(/hi/) // returns [hi]


Edit: Also, just to be clear, there's nothing wrong with your regex other than the quotes. You may want to consider using /g (i.e. /gi at the end) if you need to replace more than one match, but that's it.


[#91258] Friday, July 8, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jonrened

Total Points: 627
Total Questions: 114
Total Answers: 99

Location: Zimbabwe
Member since Thu, Jul 21, 2022
2 Years ago
jonrened questions
Mon, Nov 2, 20, 00:00, 4 Years ago
Tue, May 19, 20, 00:00, 4 Years ago
Tue, Jan 21, 20, 00:00, 4 Years ago
Thu, Nov 7, 19, 00:00, 5 Years ago
;