Solved

DOMString

Posted on 2013-12-05
4
272 Views
Last Modified: 2013-12-09
Hello,
I'm at <link no-authoritative competing site deleted by COBOLdinosaur, Topic Advisor>  where I find this;
"<tag>asd</tag>".match(/<tag>[^<]+<\/tag>/);
Is understanding this expression part of knowing JQuery, or some other language?
Or is it a regular expression.  In particular this part [^<]
I have been wanted to get to learn jquery, but have not gotten to it.
thanks
0
Comment
Question by:chima
  • 2
4 Comments
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 100 total points
ID: 39698927
this is only javascript code

match use a regular expression :

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match

If it was jQuery you should see some $ or jQuery somewhere
0
 
LVL 9

Accepted Solution

by:
Derek Jensen earned 400 total points
ID: 39703448
The regular expression matches, specifically, "tag" tags. First I'll explain what it's doing and then see if I can make it a bit more useful. Feel free to paste this code directly into your page--inside your <script> tags, of course. ;-)

// First, let's break it out so it's a little bit more readable.

var myHTML = "<tag>asd</tag>";
myHTML.match(/<tag>[^<]+<\/tag>/); // This line alone doesn't do anything useful
                                   // Let's see what it's doing:
var foundMatches = myHTML.match(/<tag>[^<]+<\/tag>/);
alert(foundMatches[0]);
/* Well that's not very useful; we already know it matches! 
But *how* do we know it matches...??

First let's take everything inside the delimiters (forward slashes) 
  and break them up into their "atomic" components:
<tag>      Look for: a string of chars that equals *specifically* "<tag>".
           Since this doesn't have any wildcards (* + ? .) it ends before 
           the opening bracket...
[^<]+      which looks for: 
               one or more string of chars (+) 
               that are NOT ("[^...]" equals "!=...") a less than (<) ...
<\/tag>    and finally find us another specific "<tag>" string of chars.

Well that's *still* not very useful! So let's see if we can fix that...
*/
myHTML = "<div ref='this may be text that we can't know beforehand' id='blah'>
There's some TEXT in here!!
</div>";
foundMatches = myHTML.match(/<[^>]+?>([^<]+?)<\/[^>]+?>/sig);
alert(foundMatches[1]);

/*Now* we're talking! So let's see how this one is different:
<          Simple, look for a less than...
[^>]+?     We already know what this one does...but why the "?"? 
           All that does is turn our '+' wildcard into a non-greedy form, 
           which will match as *little* text as possible 
           instead of as *much* text as possible...
>          then look for a greater than...
([^<]+?)   then same as above--but wait! What's the parentheses doing?? 
           All that says to do is store everything that's found by the regex 
           inside the parentheses in its own index when it returns 
           the found matches. Moving on...
<\/        now look for another less than, followed by a forward slash. 
           The backslash in front of the forward slash says,
               "This is Not my delimiter marking the end of the regex, 
               but a character to look for"...
[^>]+?     again, look for anything but greater than...
>          and finally look for a greater than.

Now we look outside of the delimiters at the trailing chars, s, i & g.
These are called switches, and they turn on or off certain functionality
that you may or may not want, depending on what you're looking for.
The first one, "s", says to wildcards to match any char *including newlines*!
This is off by default. The second one, "i", we don't actually need, but it's
very useful to know, and probably the most used switch in regexes,
and it turns off case sensitivity, or makes the regex 'case-Insensitive'
The last one we don't need either, since our string only contains one tag,
but it makes the regex match as many times as possible within the given string,
or, Globally.
*/

Open in new window

0
 

Author Closing Comment

by:chima
ID: 39706565
Thank you both. bigdogdman great explanation.
0
 
LVL 9

Expert Comment

by:Derek Jensen
ID: 39706652
Glad I could help. :-)
Happy regexing!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now