Regex Help to Strip Some Text

V

vunet.us

Please, help me with regular expression to grab image source in any
text string using the code below. I only need the regex to be plugged
in:

var strText = "some text <img src='image.jpg'> and some text";

var separateBy = ", ";
var result = ""; // if no match, use this
var allMatches = strText.match(........--->reg exp<---........);
if (allMatches) {
result = "";
for (var i = 0; i < allMatches.length; i++) {
if (i != 0) result += separateBy;
result += allMatches;
}
}
alert(result)

Result should be: image.jpg
Thanks
 
P

Peter Michaux

Please, help me with regular expression to grab image source in any
text string using the code below. I only need the regex to be plugged
in:

var strText = "some text <img src='image.jpg'> and some text";

var separateBy = ", ";
var result = ""; // if no match, use this
var allMatches = strText.match(........--->reg exp<---........);
if (allMatches) {
result = "";
for (var i = 0; i < allMatches.length; i++) {
if (i != 0) result += separateBy;
result += allMatches;
}
}
alert(result)

Result should be: image.jpg



What did you try for a RegExp?

(Does anyone here like constructing RegExps?)

Peter
 
V

vunet.us

Please, help me with regular expression to grab image source in any
text string using the code below. I only need the regex to be plugged
in:
var strText = "some text <img src='image.jpg'> and some text";
var separateBy = ", ";
var result = ""; // if no match, use this
var allMatches = strText.match(........--->reg exp<---........);
if (allMatches) {
result = "";
for (var i = 0; i < allMatches.length; i++) {
if (i != 0) result += separateBy;
result += allMatches;
}
}
alert(result)

Result should be: image.jpg

What did you try for a RegExp?

(Does anyone here like constructing RegExps?)

Peter


what would you recommend: changing group or using another method?
 
E

Evertjan.

wrote on 05 jul 2007 in comp.lang.javascript:
Please, help me with regular expression to grab image source in any
text string using the code below. I only need the regex to be plugged
in:

var strText = "some text <img src='image.jpg'> and some text";
[...]

Result should be: image.jpg

var strText = "some text <img src='image.jpg'> and some text";
Result = strText.replace(/.*/,'image.jpg');

This works as requested

Perhaps this will appeal to you:

var strText = "some text <img src='image.jpg'> and some text";
strText =
strText.replace(/(some text <img src=')|('> and some text)/g,'');

Or even:

var strText = "some text <img src='image.jpg'> and some text";
strText =
strText.replace(/(.*?<img src=')|('>.*)/g,'');
 
V

vunet.us

wrote on 05 jul 2007 in comp.lang.javascript:


Please, help me with regular expression to grab image source in any
text string using the code below. I only need the regex to be plugged
in:
var strText = "some text <img src='image.jpg'> and some text";
[...]

Result should be: image.jpg

var strText = "some text <img src='image.jpg'> and some text";
Result = strText.replace(/.*/,'image.jpg');

This works as requested

Perhaps this will appeal to you:

var strText = "some text <img src='image.jpg'> and some text";
strText =
strText.replace(/(some text <img src=')|('> and some text)/g,'');

Or even:

var strText = "some text <img src='image.jpg'> and some text";
strText =
strText.replace(/(.*?<img src=')|('>.*)/g,'');

This seems interesting:
var strText = "some text <img src='image.jpg'> and some text";
strText =
strText.replace(/(.*?<img src=')|('>.*)/g,'');

But how exactly will it work? What if I have in upper case <IMG
BORDER=0 SRC='BLAH'>?
 
E

Evertjan.

wrote on 06 jul 2007 in comp.lang.javascript:
wrote on 05 jul 2007 in comp.lang.javascript: [.......]

var strText = "some text <img src='image.jpg'> and some text";
strText =
strText.replace(/(.*?<img src=')|('>.*)/g,'');

[please do not quoe signatures n usenet]
This seems interesting:
var strText = "some text <img src='image.jpg'> and some text";
strText =
strText.replace(/(.*?<img src=')|('>.*)/g,'');
Indeed

But how exactly will it work?

Did you test it?
Did you do some reserch into regex?

I don't think I will explain regex to you, better read a tutorial.
What if I have in upper case <IMG
BORDER=0 SRC='BLAH'>?

strText =
strText.replace(/(.*?<img src=')|('>.*)/ig,'');
 
R

ron.h.hall

wrote on 05 jul 2007 in comp.lang.javascript:
Please, help me with regular expression to grab image source in any
text string using the code below. I only need the regex to be plugged
in:
var strText = "some text <img src='image.jpg'> and some text";
Result should be: image.jpg
var strText = "some text <img src='image.jpg'> and some text";
Result = strText.replace(/.*/,'image.jpg');
This works as requested
Perhaps this will appeal to you:
var strText = "some text <img src='image.jpg'> and some text";
strText =
strText.replace(/(some text <img src=')|('> and some text)/g,'');
var strText = "some text <img src='image.jpg'> and some text";
strText =
strText.replace(/(.*?<img src=')|('>.*)/g,'');

This seems interesting:
var strText = "some text <img src='image.jpg'> and some text";
strText =
strText.replace(/(.*?<img src=')|('>.*)/g,'');

But how exactly will it work? What if I have in upper case <IMG
BORDER=0 SRC='BLAH'>?

var m,
separateBy = ", ",
result =[]
;
while(m = /<img[^>]+src=('|")([^'"]+)/ig.exec(strText)) {
result.push(m[2]);
}
alert(result.join(separateBy));
 
V

vunet.us

wrote on 06 jul 2007 in comp.lang.javascript:


wrote on 05 jul 2007 in comp.lang.javascript:
[.......]
var strText = "some text <img src='image.jpg'> and some text";
strText =
strText.replace(/(.*?<img src=')|('>.*)/g,'');

[please do not quoe signatures n usenet]
This seems interesting:
var strText = "some text <img src='image.jpg'> and some text";
strText =
strText.replace(/(.*?<img src=')|('>.*)/g,'');
Indeed

But how exactly will it work?

Did you test it?
Did you do some reserch into regex?

I don't think I will explain regex to you, better read a tutorial.
What if I have in upper case <IMG
BORDER=0 SRC='BLAH'>?

strText =
strText.replace(/(.*?<img src=')|('>.*)/ig,'');

thanks a lot. it catches the first instance in my string below exactly
how I need:
var strText = "ab cd efg hig klmnop <img src='file.jpg'> ot her"+
" e <IMG BORDER=0 SRC='www.g.com/file2.jpg'>";
 
V

vunet.us

wrote on 05 jul 2007 in comp.lang.javascript:
Please, help me with regular expression to grab image source in any
text string using the code below. I only need the regex to be plugged
in:
var strText = "some text <img src='image.jpg'> and some text";
[...]
Result should be: image.jpg
var strText = "some text <img src='image.jpg'> and some text";
Result = strText.replace(/.*/,'image.jpg');
This works as requested
Perhaps this will appeal to you:
var strText = "some text <img src='image.jpg'> and some text";
strText =
strText.replace(/(some text <img src=')|('> and some text)/g,'');
Or even:
var strText = "some text <img src='image.jpg'> and some text";
strText =
strText.replace(/(.*?<img src=')|('>.*)/g,'');
This seems interesting:
var strText = "some text <img src='image.jpg'> and some text";
strText =
strText.replace(/(.*?<img src=')|('>.*)/g,'');
But how exactly will it work? What if I have in upper case <IMG
BORDER=0 SRC='BLAH'>?

var m,
separateBy = ", ",
result =[]
;
while(m = /<img[^>]+src=('|")([^'"]+)/ig.exec(strText)) {
result.push(m[2]);
}
alert(result.join(separateBy));

This does a great job catching all instances in my modified version:
f
unction start(){

var strText = "ab cd efg hig klmnop <img src='file.jpg'> ot her"+
" e <IMG SRC='www.g.com/file2.jpg' BORDER=0>";
alert(strText.replace(/(.*?<img src=')|('>.*)/ig,''));
var separateBy = ", ";
var result = ""; // if no match, use this
var allMatches = strText.match(/<img[^>]+src=('|")([^'"]+)/ig);
if (allMatches) {
result = "";
for (var i = 0; i < allMatches.length; i++) {
if (i != 0) result += separateBy;
result += allMatches;
}
}
alert(result)

}



however, it returns some prefixes like: <img src='file.jpg instead of
file.jpg.
I'll try to combine your answers to come up with what I need.
Thanks a lot.
 
R

ron.h.hall

On Jul 5, 3:04 pm, (e-mail address removed) wrote:
var m,
separateBy = ", ",
result =[]
;
while(m = /<img[^>]+src=('|")([^'"]+)/ig.exec(strText)) {
result.push(m[2]);
}
alert(result.join(separateBy));

This does a great job catching all instances in my modified version:
f
unction start(){

var strText = "ab cd efg hig klmnop <img src='file.jpg'> ot her"+
" e <IMG SRC='www.g.com/file2.jpg'BORDER=0>";
alert(strText.replace(/(.*?<img src=')|('>.*)/ig,''));
var separateBy = ", ";
var result = ""; // if no match, use this
var allMatches = strText.match(/<img[^>]+src=('|")([^'"]+)/ig);
if (allMatches) {
result = "";
for (var i = 0; i < allMatches.length; i++) {
if (i != 0) result += separateBy;
result += allMatches;
}
}
alert(result)

}

however, it returns some prefixes like: <img src='file.jpg instead of
file.jpg.


Well of course it doesn't perform as expected in that usage.

The regular expression given wasn't intended to re-deployed in
String.match as you have decided to do. Why would you not take the
example and begin to work from there, testing to see if it met your
requirements?

Note it is, nonetheless, an example, and could fail to pick up some
source URL's if, e.g., the URL was not enclosed in quotes. Also, it
would be possible for the ">" test to fail in some cases, so some
caution is advised.
 
V

vunet.us

On Jul 6, 12:04 am, (e-mail address removed) wrote:
On Jul 5, 3:04 pm, (e-mail address removed) wrote:
But how exactly will it work? What if I have in upper case <IMG
BORDER=0 SRC='BLAH'>?
var m,
separateBy = ", ",
result =[]
;
while(m = /<img[^>]+src=('|")([^'"]+)/ig.exec(strText)) {
result.push(m[2]);
}
alert(result.join(separateBy));
This does a great job catching all instances in my modified version:
f
unction start(){
var strText = "ab cd efg hig klmnop <img src='file.jpg'> ot her"+
" e <IMG SRC='www.g.com/file2.jpg'BORDER=0>";
alert(strText.replace(/(.*?<img src=')|('>.*)/ig,''));
var separateBy = ", ";
var result = ""; // if no match, use this
var allMatches = strText.match(/<img[^>]+src=('|")([^'"]+)/ig);
if (allMatches) {
result = "";
for (var i = 0; i < allMatches.length; i++) {
if (i != 0) result += separateBy;
result += allMatches;
}
}
alert(result)

however, it returns some prefixes like: <img src='file.jpg instead of
file.jpg.


Well of course it doesn't perform as expected in that usage.

The regular expression given wasn't intended to re-deployed in
String.match as you have decided to do. Why would you not take the
example and begin to work from there, testing to see if it met your
requirements?

Note it is, nonetheless, an example, and could fail to pick up some
source URL's if, e.g., the URL was not enclosed in quotes. Also, it
would be possible for the ">" test to fail in some cases, so some
caution is advised.


absolutely, that is a good way for me to begin to come up with what I
need having what I got.
Thanks a lot
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,279
Latest member
LaRoseDermaBottle

Latest Threads

Top