473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Simple RegEx Help

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
7 1693
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
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
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
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
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
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
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 n–1 corresponding
| to the first elements of the results of all matching invocations of
| RegExp.prototype.exec.
HTH

PointedEars
Oct 24 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: live your lives | last post by:
i am trying to validate a simple username textbox using RegularExpressionValidator: TextBox tbUserName = new TextBox(); tbUserName.ID = "tbUserName"; string strPatternUserName = @"\W"; //...
7
by: Razzie | last post by:
Hey all, Decided to give a shot at Regular expressions - need a bit of help :) I can't seem to find the right regex for matching words like "*test*" or *somevalue*" - in short, all words...
2
by: Brian Henry | last post by:
Hi everyone, I never have worked much with regex (trying to learn it now myself) but I have a simple problem that I need to solve.. Say I have numbers like this 00023432-234...
5
by: FBergemann | last post by:
I use SunOS 5.8, gcc 3.3.2, boost 1.33.1. I have build the entire boost package and try to compile a simple example: #include <iostream> #include <string> #include <boost/regex.hpp //...
4
by: bg_ie | last post by:
I have the following Regex - oldString = @"C:\ta"; Regex regexEndsInBackslash = new Regex(oldString + @"\\", RegexOptions.Singleline | RegexOptions.IgnoreCase); Any ideas why none of the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.