Results For Tag: javascript

May 18th, 2019Whats new?

30 Scripts For Galleries, Slideshows and Lightboxes

Family photos, vacation snapshots or creative artistic works: whatever images you have to present, you can present them in a variety of ways. On a big screen, in slide shows or in a thumbnails gallery. However, to convey the message of presented data effectively, it ’s important to offer it in an attractive and intuitive way.

14 rules for fast web pages

“Steve Souders of Yahoo’s “Exceptional Performance Team” gave an insanely great presentation at Web 2.0 about optimizing website performance by focusing on front end issues. […] the Web 2.0 talks have just been put up and the ppt is fascinating and absolutely a must-read for anyone involved in web products.”

Mmm! I love raisins made with SELECT * FROM [Equipment Table]! (pic)

How come my chocolate covered raisins never come with any SQL?

Top 5 javascript frameworks

A list of the top 5 javascript frameworks and features.

How dumb does Microsoft think we are?

Not only will they not tell us what those patents are hidden in Linux, they also won’t tell us how they found them. Argh!

The Ultimate Linux Reference Guide for Newbies

Very nice and complete linux commands reference.

25 Killer Code Snippets every Good Designer Should See

Round up 25 of the Best CSS, scripts, html, javascript, Ajax and widgets that you can use on on your website or blog (Part 5) ….

5 Techniques for Enhancing Contrast in Digital Photos

Extensive advice for increasing contrast in photos. Very good tips and tricks.

May 5th, 2019Whats new?

The Javascript Programming Language

“Excellent presentation and great insight on the history and basics. Douglas rocks, yahoo is real lucky to have him!”

The $25,000,000,000 Eigenvector: The Linear Algebra Behind Google

An undergraduate-oriented explanation of the beautiful and simple linear algebra that lies behind an important facet of Google’s PageRank algorithm. The page has some demo code.

Basics of Compiler Design

“I have taught an undergraduate compilers course for over a decade. In the last several years, I have used my own textbook “Basics of Compiler Design”. I have now decided to make this available online.”

Dell’s Ubuntu PCs won’t include Windows emulation

Dell does not plan to include software such as Wine, which lets users run Windows programs on Linux, with its PCs that will soon be preloaded Ubuntu Linux. “I do not want to position Ubuntu and Linux as a cheap alternative to Windows,” Ubuntu founder Mark Shuttleworth told eWEEK.com.

Absolutely HUGE List of Color Related Sites!

Someone put together a list of just about every color related resource (worth mentioning) in the known universe. Designers rejoice!

Vista draining laptop batteries, patience

Some of Microsoft’s most important customers aren’t happy with the battery life offered by notebooks running Windows Vista.

Ubuntu Posters for your wall or office door!

Want to show off your Ubuntu pride? Want to help spread awareness there is such a thing as Ubuntu? Or do you just want to decorate your own room? Perhaps one of these community-produced posters are just what you need. Twice the size of the last run, shipped anywhere around the world. You can throw in a few laptop stickers/case badges as well.

Getting Skewed Perspective Screenshots With Photoshop

Dave Shea has an excellent tutorial over on Mezzoblue that shows how to spice up your screenshots by giving them some perspective and a touch of focal blur.

Introduction

Regular expressions is a powerful tool for the incoming data processing. The problem demanding replacement or text search, can be successfully solved by this “language inside language”. And thought maximum effect from the regular expressions might be achieved by using server languages, it is not no point to undervalue the opportunities of this client’s utility.

Basic concepts

Regular expression is a tool for lines or symbols strings processing, which determines text template.

Modifier is designed for regular expression “supervision”.

Metacharacters are special symbols, which serve as commands of regular expressions language.

Regular expression is worked out as usual variable, just it is used slash instead of quotes, e.g.:var reg=/reg_expression/

Simple templates are such templates, which do not need any special symbols.

Let us say, our task is to change all the letters “p” (both capital and lowercase) to Latin letter “R” in phrase Regular expressions.

We create template var reg=/p/ and realize it with replace method

<script language=”JavaScript”>
var str=”Regular expressions”
var reg=/r/
var result=str.replace(reg, “R”)
document.write(result)
</script>

As a result we receive <<RegulaR expressions>>, change was made only in first occurrence of letter “r” according to match-case.
But this result doesn’t suit under conditions of our task… Here we need modifiers “g” and “i”, which may be used both separately and altogether. These modifiers are put inside the template of regular expression, after slash. They have the following values:

modifier “g” specifies line search as “global”, that is to say in our case change happens with all the occurrences of letter “p”. Now the template looks this way: var reg=/r/g. If we insert it into our code:

<script language=”JavaScript”>
var str=”Regular expressions”
var reg=/r/g
var result=str.replace(reg, “R”)
document.write(result)
</script>

So we receive <<RegulaR expRetions>>.

modifier “i” specifies case-insensitive line search, so if we add this modifier into our template var reg=/r/gi, after script processing we receive required result of our task:<<regular expretions>>.

Special symbols (Metacharacters)

Metacharacters specify symbols type of the required line, type of external environment of line in text, as well as quantity of the symbols of separate type in browseable text. That is why it is possible to divide metacharacters into three groups:

  • Metacharacters of coincidence search.
  • Quantified metacharacters.
  • Metacharacters of position.
  • Metacharacters of coincidence search:

    \b word board, which specifies condition, under which the template is to be processed at the end or at the beginning of the words.

    \B not word board, which specifies condition, under which the template is not processed at the end or at the beginning of the words.

    \d digit from 0 to 9.

    \D not a digit.

    \s single empty symbol, corresponding to space symbol.

    \S single nonempty symbol, any symbol other than space.

    \w digit, letter of flatworm.

    \W not digit, letter of flatworm.

    . any symbol, any signs, letter, digits, etc.

    [ ] symbol series, specifies condition, under which template has to be processed under any symbols coincidence put into square brackets.

    [^ ], set of non-occurransing symbols, specifies condition, under which template has not to be processed under any symbols coincidence put into square brackets.

    Quantified metacharacters:

    * Zero or more times.

    ? Zero or one time

    + One or more times.

    {n} exactly n times.

    {n,} n or more times.

    {n,m} at least n times but less than m times.

    Metacharacters of position:

    ^ at the line beginning.

    $ at the end of line.

    Some methods of work with templates

    replace — this method we had already used at the beginning of the article, it is assigned for the sample search and change of found subchain to new subchain.

    test — this methods checks out whether coincidence in the line takes place towards the template, and retrieves false, if comparison with the sample failed, otherwise true.

    For example:

    <script language=”JavaScript”>
    var str=”JavaScript”
    var reg=/PHP/
    var result=reg.test(str)
    document.write(result)
    </script>

    As a result displays false, as the line “JavaScript” is not equal to line “PHP“.

    Also method test may retrieve any other line assigned by the programmer to true or false.

    For example:

    <script language=”JavaScript”>
    var str=”JavaScript”
    var reg=/PHP/
    var result=reg.test(str) ? “Line coincided”: “Line did not coincide”
    document.write(result)
    </script>

    In this case a result will be displayed as “Line did not coincide”.

    exec — this method processes matching of the line with the sample, assigned by the template. If correlation with the sample failed, the meaning null is retrieved. Otherwise the result is subchains array, corresponding to specified sample. /*The first element of the array will be equal to initial line complying with the specified template*/

    for example:

    <script language=”JavaScript”>
    var reg=/(\d+).(\d+).(\d+)/
    var arr=reg.exec(”I was born on 15.09.1980″)
    document.write(”Date of birth: “, arr[[ ,]] “< br>”)
    document.write(”Day of birth: “, arr, “< br>”)
    document.write(”Month of birth: “, arr, “< br>”)
    document.write(”Year of birth: “, arr, “< br>”)
    </script>

    As a result we receive four lines:

    Date of birth: 15.09.1980
    Day of birth: 15
    Month of birth: 09
    Year of birth: 1980

    Conclusion

    Far not all the abilities and advantages of the regular expressions are described in this article.
    For deeper studying of this matter I’d recommend you to learn RebExp object.
    Also I’d like to point out that syntax of regular expressions is the same in JavaScript and PHP.
    For example if you need to check out the correctness of e-mail input, the regular expression looks the same
    way for JavaScript and PHP: /[0-9a-z_]+@[0-9a-z_^.]+.[a-z]{2,3}/i.

General description

Regular expressions are represented as samples for searching set symbols combinations in text lines (this search refers to comparison to the sample). There are two ways of assignment of variables regular expressions, namely:

  • Usage of the object initializer: var re = /pattern/switch?.
  • Using builder RegExp:
  • var re = new
    RegExp(”pattern”[,”switch”]?).
    //Here pattern - regular expression, switch – unrequired search options.

    Object initializers, eg, var re = /ab+c/, are to be applied in cases, when the value of regular expression stays constant in script service. Such regular expressions compile during script loading, so they execute faster.
    Builder call-up, eg, var re = new RegExp(”ab+c”), is to be used in cases when value of variable is going to change. If you are intended to use regular expression several times, then there has sense to compile it by “compile” method for much more effective samples search.

    When creating regular expression it is necessary to take into account, that putting it into quotes implies necessity to use escape-consistency, as well as in any other string constant.

    For example, two following expressions are equivalent:

    var re = /\w+/g;
    var re = new RegExp(”\\w+”, “g”); // In the line “\” changes to “\\”

    Note: regular expression cannot be empty: two symbols // at a run fix the comment beginning. So in order to create empty regular expression use /.?/.

    Regular expressions are used by methods ‘exec’ and ‘test’ of RegExp object and by methods ‘match’, ‘replace’, ’search’ and ’split’ of String object. If we need just to check whether assined string contains substring, relevant to the sample, the following methods are used:a href=”/cgi-bin/print.pl?id=118-4.html#505″ mce_href=”/cgi-bin/print.pl?id=118-4.html#505″ class=intext>test or ’search’. But if we need to extract subchain (or subchains) relevant to the sample, we need to use methods ‘exec’ or ‘match’. ‘Replace’ method provides search of assigned subchain and its change into another chain, and ’split’ method allows to break chain into several subchains, being based on regular expression or common text chain. Detailed information about using of regular expressions is displayed in descriptions of corresponding methods.

    Syntax of regular expressions

    Regular expression may consist of common symbols; in this case it will correspond to assigned combination of symbols in chain. For example, expression /com/ corresponds to marked subchains in the following chains: «clum», «sweet-tooth», «fleet headquater». Though, flexibility and power of regular expressions gives possibility to use special symbols, which are listed in the following table.

    Special symbols in regular expressions:

    For the symbols, which are usually interpreted by literal, means that next symbol is special. For example, /n/corresponds to letter n, and /\n/ corresponds to symbol of line advance.
    For the symbols, which are usually interpreted as special, means that symbol has to be interpreted by literal. For example, /^/ means the beginning of the chain, and /\^/ just corresponds to symbol ^. /\\/ corresponds to reverse slash.

    ^ - Corresponds to the chain beginning.

    $ - Corresponds to the chain end.

    * - Corresponds to iterate of previous symbol zero or more times.

    + - Corresponds to iterate of previous symbol one or more times.

    ? - Corresponds to iterate of previous symbol zero or one times.

    . - Corresponds to any symbol, besides symbol of new chain.

    (pattern) - Corresponds to chain pattern and memorizes found correspondence.

    (?:pattern) - Corresponds to chain ‘pattern’, but does not memorize found correspondence. Is used for grouping of sample’s parts, e.g. /ca(?:t|ttle)/ -
    is a short writing of the expression /cat|cattle/.

    (?=pattern) - Corresponding with “foreseeing”, happens under matching of line pattern without memorizing of found matching. For example /Windows (?=95|98|NT|2000)/ corresponds to “Windows ” in chain “Windows 98″, but mismatches in chain “Windows 3.1″. After correlation search continues from the position coming next after found match without foreseeing.

    (?!pattern) - Corresponding with «foreseeing», happens by mismatching of chain ‘pattern’ without memorizing of found correspondence. For example, /Windows (?!95|98|NT|2000)/ corresponds to “Windows ” in chain “Windows 3.1″, but mismatches in chain “Windows 98″. After correlation search continues from the position coming next after found mismatch, without foreseeing.

    x|y - Corresponds to x or y.

    {n} - n - nonnegative number. Corresponds equally to n occurrences of previous symbol.

    {n,} - n - nonnegative number. Corresponds to n or more occurrences of previous symbol. /x{1,}/ is equivalent to /x+/. /x{0,}/ is equivalent to
    /x*/.

    {n,m} - n and m – nonnegative number. Corresponds not less than to n but not more than to m occurrences of previous symbol. /x{0,1}/ is equivalent to /x?/.

    [xyz] - Corresponds to any symbol put into square brackets.

    [^xyz] - besides ones put into square brackets.

    [a-z] - Corresponds to any symbol in the indicated extend.

    [^a-z] - Corresponds to any symbol, besides those in the indicated extend.

    \b - Corresponds to word bounder that is position between word and space or line advance.

    \B - Corresponds to any position besides word bounder.

    \ñX - Corresponds to symbol Ctrl+X. E.g., /\cI/ is equivalent to /\t/.

    \d - Corresponds to digit. Equivalent to [0-9].

    \D - Corresponds to non-numerical character. Equivalent to [^0-9].

    \f - Corresponds to format transfer symbol (FF).

    \n - Corresponds to line feed symbol (LF).

    \r - Corresponds to carriage return symbol (CR).

    \s - Corresponds to space symbol. Equivalent to /[ \f\n\r\t\v]/.

    \S - Corresponds to any non-space symbol. Equivalent to /[^ \f\n\r\t\v]/.

    \t - Corresponds to tabulation symbol (HT).

    \v - Corresponds to vertical tabulation symbol (VT).

    \w - Corresponds to Latin letter, digit or flatworm. Equivalent to /[A-Za-z0-9_] /.

    \W - Corresponds to any symbol, besides Latin letter, digit or flatworm. Equivalent to /[^A-Za-z0-9_] /.

    \n - n - positive number. Corresponds to n memorized chain. Is calculated by counting left round brackets. It is equivalent to \0n, if quantity of left round brackets is less than n.

    \0n - n - octal number, less than 377. Corresponds to symbol with octal code n. E.g., /\011/ is equivalent to /\t/.

    \xn - n - hex number, consisting of two digits. Corresponds to symbol with hex code n. E.g., /\x31/ corresponds to /1/.

    \un - n - hex number, consists of four digits. Corresponds to symbol Unicode with the hex number n. For example, /\u00A9/ is equivalent to /©/.

    Regular expressions are calculated the same way as other JavaScript expressions, that is to say with account of operations priority: the operations with higher priority are performed first. If the operations have equal priority, they are performed from left to right. In the following table the operations of regular expressions are listed in descending order of their priority. The operations located in one chain have equal priority.

    Operations:

    \
    () (?:) (?=) (?!) []
    * + ? . {n} {n,} {n,m}
    ^ $ \metacharacter
    |

    Search options:

    While creating of regular expression we can indicate additional search options:

    * i (ignore case). Not to recognize lowercase and capital letters.

    * g (global search). All sample occurrences global search.

    * m (multiline). Multi-line search.

    * Any combinations of these three options, e.g. ig or gim.

    Let us give few examples. Regular expressions recognize lowercase and capital letters. So the following script

    var s = “Learning JavaScript language”;
    var re = /JAVA/;
    var result = re.test(s) ? “” ” : “” not “;
    document.write(”Chain “” + s + result + “corresponds to sample ” + re);

    displays the following text on the screen:

    Line “Learning JavaScript language” mismatches /JAVA/ sample

    Now if we change the second line of the example to var re = /JAVA/i;, the following text appears on the screen:

    Line “Learning JavaScript language” corresponds to /JAVA/i sample

    Now let’s analyze global search option. Usually it is used by ‘replace’ method in sample search and changing found subchain to new one. The matter is that on default this method changes only fir found subchain and retrieves the received result. Let’s examine the following script:

    var s = “We write script on JavaScript, ” +
    “but JavaScript is not a unique script language.”;
    var re = /JavaScript/;
    document.write(s.replace(re, “VBScript”));

    It displays the text, which for certain mismatches with the desired result:

    We write scripts on VBScript, but JavaScript is not a unique script language.

    In order to change all the occurrences of “JavaScript” chain to
    “VBScript”, we need to change the meaning of regular expression to var
    re = /JavaScript/g;. The resulting line looks as follows:

    We write scripts in VBScript, but VBScript is not a unique script language.

    At last, the multi-line search option allows making comparison with the line expression sample, connected by break line symbols. On default comparison with the sample stops, if break line symbol is found. This option overcomes specified limitation and provides sample search throughout all the initial line. It also influences some special symbols interpretation in regular expressions, namely:

    * Usually symbol ^ is associated only with the first line element. If multi-line search option is included, it is compared with any line element, staying after break line symbol.

    * Usually symbol $ is associated only with the last line element. If multi-line search option is included, it is compared with any line element, which is break line symbol.

    Memorizing of found subchains

    If the part of regular expression is put in round brackets, corresponding subchain is memorized for further use. For the access to memorized subchains use the attributes $1, :, $9 of RegExp object or elements of array variable, retrieved by exec and match methods. In the last case the quantity of found and memorized lines is unlimited.

    For example, the following script uses replace method for derangement of the words in line. Attributes $1 and $2 are used to change found text.

    var re = /(\w+)\s(\w+)/;
    var str = “Regular Expressions”;
    document.write(str.replace(re, “$2, $1″))

    This script will display the following text on the screen:

    Regular, Expressions

    Read Part 2


© 2007 - 2021 Web Development | iKon Wordpress Theme by TextNData | Powered by Wordpress | rakCha web directory
XHTML CSS RSS

Next day courier services
personalised gifts
Buy quality cheap bedroom furniture from konteaki furniture