Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

DOMString

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
chima
Asked:
chima
  • 2
2 Solutions
 
leakim971PluritechnicianCommented:
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
 
Derek JensenCommented:
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
 
chimaAuthor Commented:
Thank you both. bigdogdman great explanation.
0
 
Derek JensenCommented:
Glad I could help. :-)
Happy regexing!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now