Blainer
Blainer

Reputation: 2712

regex to capture html content

How would I write this to store the text "allofthistextcaptured" with regex? i dont want "_poster" to be captured. The below javascript is exactly what I need, I just need some regex at the end to capture all the text in the src tag EXCEPT "_poster".

html

<div class="postImage"><img src="allofthistextcaptured_poster.jpg" title="" width="640" height="385" /></div>

js i have so far

var text = $(this).siblings('.postImage').find('img').attr('src');

Upvotes: 0

Views: 272

Answers (3)

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79840

Just a generic replace using regEx to strip b/w k and . that is anything after _ and before .

.replace (/(_[^.*]*)/, '');

DEMO

Upvotes: 0

hexparrot
hexparrot

Reputation: 3427

Seeing as the searchable string is reduced to only the filename, you can use this regex to capture the name and the extension:

([\w]+)(?>_poster)\.([\w]+)

Upvotes: 0

Jasper
Jasper

Reputation: 76003

Will a simple replace() work for you?

var text = $(this).siblings('.postImage').find('img').attr('src').replace('_poster', '');

If you want to use RegExp then you can do something like this:

var text = $(this).siblings('.postImage').find('img').attr('src').replace(/_poster./, '.');

Which looks for _poster. and replaces it with . but this is pretty-much the same thing as the first example.

Here's a demo: http://jsfiddle.net/exAXw/

Upvotes: 4

Related Questions