Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

DOMString

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Head to not include on scroll 4 22
jQuery on Submit 4 36
Button function on table is in trouble 3 20
Set time on Session (ASP) 14 21
This article discusses four methods for overlaying images in a container on a web page
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

765 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