Solved

DOMString

Posted on 2013-12-05
4
268 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

760 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

20 Experts available now in Live!

Get 1:1 Help Now