By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,238 Members | 1,609 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,238 IT Pros & Developers. It's quick & easy.

Simple RegEx Help

P: n/a
How can I get the text after matching string is found:

var str = "1111>AAAA<2222>BBBB<3333>CCCC";

if(str.indexOf("2222>")){

//how to get "BBBB" value following my "2222>" but before "<3333"
or any < sign?

}

Please, recommend a RegEx if possible. Thanks.

Oct 24 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
VUNETdotUS wrote on 24 okt 2007 in comp.lang.javascript:
How can I get the text after matching string is found:

var str = "1111>AAAA<2222>BBBB<3333>CCCC";

if(str.indexOf("2222>")){

//how to get "BBBB" value following my "2222>" but before "<3333"
or any < sign?

}

Please, recommend a RegEx if possible. Thanks.
<script type='text/javascript'>

var stri = "1111>AAAA<2222>BBBB<3333>CCCC";

var search = "2222";

var re = new RegExp('('+search+'>)([^<$]*)','');

var result = stri.match(re);

result = (result)?result[2]:'Not found';

alert(result);

</script>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 24 '07 #2

P: n/a
On 24 Oct, 16:09, VUNETdotUS <vunet...@gmail.comwrote:
How can I get the text after matching string is found:

var str = "1111>AAAA<2222>BBBB<3333>CCCC";

if(str.indexOf("2222>")){

//how to get "BBBB" value following my "2222>" but before "<3333"
or any < sign?

}

Please, recommend a RegEx if possible. Thanks.
pattern = /2222>(.*)</;
alert(pattern.exec(str)[1]);

Oct 24 '07 #3

P: n/a
On Oct 24, 11:56 am, "Evertjan." <exjxw.hannivo...@interxnl.net>
wrote:
VUNETdotUS wrote on 24 okt 2007 in comp.lang.javascript:
How can I get the text after matching string is found:
var str = "1111>AAAA<2222>BBBB<3333>CCCC";
if(str.indexOf("2222>")){
//how to get "BBBB" value following my "2222>" but before "<3333"
or any < sign?
}
Please, recommend a RegEx if possible. Thanks.

<script type='text/javascript'>

var stri = "1111>AAAA<2222>BBBB<3333>CCCC";

var search = "2222";

var re = new RegExp('('+search+'>)([^<$]*)','');

var result = stri.match(re);

result = (result)?result[2]:'Not found';

alert(result);

</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
thank you very much!

Oct 24 '07 #4

P: n/a
On Oct 24, 11:59 am, Captain Paralytic <paul_laut...@yahoo.comwrote:
On 24 Oct, 16:09, VUNETdotUS <vunet...@gmail.comwrote:
How can I get the text after matching string is found:
var str = "1111>AAAA<2222>BBBB<3333>CCCC";
if(str.indexOf("2222>")){
//how to get "BBBB" value following my "2222>" but before "<3333"
or any < sign?
}
Please, recommend a RegEx if possible. Thanks.

pattern = /2222>(.*)</;
alert(pattern.exec(str)[1]);
thank you very much!

Oct 24 '07 #5

P: n/a
On Oct 24, 11:56 am, "Evertjan." <exjxw.hannivo...@interxnl.net>
wrote:
VUNETdotUS wrote on 24 okt 2007 in comp.lang.javascript:
How can I get the text after matching string is found:
var str = "1111>AAAA<2222>BBBB<3333>CCCC";
if(str.indexOf("2222>")){
//how to get "BBBB" value following my "2222>" but before "<3333"
or any < sign?
}
Please, recommend a RegEx if possible. Thanks.

<script type='text/javascript'>

var stri = "1111>AAAA<2222>BBBB<3333>CCCC";

var search = "2222";

var re = new RegExp('('+search+'>)([^<$]*)','');

var result = stri.match(re);

result = (result)?result[2]:'Not found';

alert(result);

</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
I wonder why if I make global search like var re = new
RegExp('('+search+'>)([^<$]*)','g'); then result[2] cannot be used but
result can?

Oct 24 '07 #6

P: n/a
VUNETdotUS wrote on 24 okt 2007 in comp.lang.javascript:
I wonder why if I make global search like var re = new
RegExp('('+search+'>)([^<$]*)','g'); then result[2] cannot be used but
result can?
Read the specs of match() on the global flag.

Programming is not a trial and error art,
but is understanding the functionality.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 24 '07 #7

P: n/a
VUNETdotUS wrote:
On Oct 24, 11:56 am, "Evertjan." <exjxw.hannivo...@interxnl.net>
wrote:
>VUNETdotUS wrote on 24 okt 2007 in comp.lang.javascript:
>>How can I get the text after matching string is found:
var str = "1111>AAAA<2222>BBBB<3333>CCCC";
if(str.indexOf("2222>")){
//how to get "BBBB" value following my "2222>" but before "<3333"
or any < sign?
}
Please, recommend a RegEx if possible. Thanks.
<script type='text/javascript'>

var stri = "1111>AAAA<2222>BBBB<3333>CCCC";

var search = "2222";

var re = new RegExp('('+search+'>)([^<$]*)','');

var result = stri.match(re);

result = (result)?result[2]:'Not found';

alert(result);

</script>
[...]
Please quote only what you are referring to.
I wonder why if I make global search like var re = new
RegExp('('+search+'>)([^<$]*)','g'); then result[2] cannot be used but
result can?
,-[ECMAScript Ed. 3 Final]
|
| 15.5.4.10 String.prototype.match (regexp)
|
| If `regexp' is not an object whose [[Class]] property is "RegExp", it is
| replaced with the result of the expression new RegExp(regexp). Let
| `string' denote the result of converting the this value to a string.
| Then do one of the following:
|
| If regexp.global is `false': Return the result obtained by invoking
| RegExp.prototype.exec (see section 15.10.6.2) on `regexp' with `string'
| as parameter.
|
| If regexp.global is `true': Set the regexp.lastIndex property to 0 and
| invoke RegExp.prototype.exec repeatedly until there is no match.
| If there is a match with an empty string (in other words, if the value
| of regexp.lastIndex is left unchanged), increment regexp.lastIndex by 1.
| Let n be the number of matches. The value returned is an array with
| the length property set to n and properties 0 through n1 corresponding
| to the first elements of the results of all matching invocations of
| RegExp.prototype.exec.
HTH

PointedEars
Oct 24 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.