Reputation: 2686
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`>
Upvotes: 0
Views: 93
Reputation: 25994
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...
Upvotes: 1
Reputation: 152266
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;
Upvotes: 3