0

I'm a totally regular expression newbie. Can anyone help me creating a regular expression in javascript in order to extract Test Timp from

<a href="/taskuri/edit/?tid=8510#?txName=&ddProject=-1&ddUser=39&ddStatus=0&ddAssigner=-1&deadline=&page=1&sortname=created&sortorder=desc">Test Timp</a><span></span`>  
5
  • 1
    how about simple a.innerText ? Commented Jan 7, 2014 at 10:38
  • can u assign Id to the A tag? Commented Jan 7, 2014 at 10:40
  • @JoranDenHouting, your version is bad and you should feel bad. There is no place for "thanks" in SO questions. Commented Jan 7, 2014 at 10:41
  • Sorry @Griwes, I clicked the wrong one... Fixed though ;) Commented Jan 7, 2014 at 10:42
  • Take a look at this topic, it may help you understand why regex is not what you should be looking: stackoverflow.com/questions/1732348/… Commented Jan 7, 2014 at 11:07

2 Answers 2

3

Regex is not what you should use to work with HTML code. Try with:

var input  = '<a href="/taskuri/edit/?tid=8510#?txName=&ddProject=-1&ddUser=39&ddStatus=0&ddAssigner=-1&deadline=&page=1&sortname=created&sortorder=desc">Test Timp</a><span></span>',
    tmp    = document.createElement("DIV"),
    output = '';

tmp.innerHTML = input; 
output =  tmp.textContent || tmp.innerText;
Sign up to request clarification or add additional context in comments.

2 Comments

the html code I get it as a string , so its not html code , it is a string
In my example it is also a string stored in input variable. I use this string to create abstract DIV element.
1

As mentioned by hsz, regex isn't the best way to achieve your goal. But since you asked to learn to build a regex, here we go:

First, you need to see what makes the text you're after "special" (i.e. how it can be found by a dump computer). In your case, it's the text between <a ...> and </a>.

So our regex will be "grab all the characters between <a ...> and </a>", so let's start with

var regex = /<a ...>THE STUFF I WANT\</a/;

Regexes get put between two / so we've already got that there. Next, we need to replace the above with things that are actually valid regex expressions. First, we need a way to refer to the stuff we want. We do this by enclosing it in parentheses, because it gives us a "group" we can later refer to:

var regex = /<a ...>(THE STUFF I WANT)\</a/;

Next, we need to replace the "STUFF I WANT" palceholder with the regex expression meaning "any bunch of characters":

var regex = /<a ...>(.+)\</a/;

The . means "any character" and + means "at least one" (you can also use * to mean "zero or more").

Next let's change the confusing / by escaping it with \:

var regex = /<a ...>(.+)\<\/a/;

Finally we replace ... with the same "a bunch of characters" expression:

var regex = /<a.+>(.+)\<\/a/;

Now that our regex is ready, lets' use it:

var str = '<a href="/taskuri/edit/?tid=8510#?txName=&ddProject=-1&ddUser=39&ddStatus=0&ddAssigner=-1&deadline=&page=1&sortname=created&sortorder=desc">Test Timp</a><span></span>';

var regex = /<a.+>(.+)\<\/a/;
var result = str.match(regex)[1];

As you can see, we just apply the regex, then grab the first defined group with the "1" index. (The data at index 0 is the entire string matched by the regex.)

And we're done!

What you need to remember in your regex, is that every character in the tested string needs to be accounted for in the regex, or it won't match...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.