Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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', '.')
share|improve this question

1 Answer 1

up vote 14 down vote accepted

'/-[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.

share|improve this answer
    
+1 bryan , nice catch –  kobe Jul 11 '11 at 7:06
1  
This helped me, thanks. –  RTF Jul 12 '13 at 19:52
    
Uhh, wasted hour on trials and rereading manuals, never noticed the absence of '' until I came here. –  Sandman4 Nov 27 '13 at 12:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.