background preloader

JavaScript RegExp Example: Online Regular Expression Tester

JavaScript RegExp Example: Online Regular Expression Tester
Feel free to test JavaScript's RegExp support right here in your browser. Obviously, JavaScript (or Microsoft's variant JScript) will need to be enabled in your browser for this to work. Since this tester is implemented in JavaScript, it will reflect the features and limitations of your web browser's JavaScript implementation. If you're looking for a general-purpose regular expression tester supporting a variety of regex flavors, grab yourself a copy of RegexBuddy.

Detecting the File Download Dialog In the Browser Updated 2011-01-28 to reference the correct question stackoverflow.com Updated 2011-10-02 With a link to a sample application on github. Updated 2013-07-01 with corrected jQuery Cookie plug-in link and syntax In the web application I work on we sometimes need to create PDF documents based on some user input. In most cases, these documents are fairly simple and quick to create, allowing us to create the documents in memory and then send them back to the requesting user with a “Content Disposition: attachment” in the HTTP response. Generating a file, however, can sometimes take a few seconds. There are some ways to work around this by writing the file to disk or some caching mechanism and then providing a separate URL endpoint to download the finished file, but these approaches require what is, in my opinion, a non-trivial amount of server side code to accomplish. The Client Side Setup Notice the ‘hidden’ input field I included in that form. The Server Side Setup Share This Post:

Learning to Use Regular Expressions Often if you find that your regular expressions are matching too much, a useful procedure is to reformulate the problem in your mind. Rather than thinking about "what am I trying to match later in the expression?" ask yourself "what do I need to avoid matching in the next part?" Often this leads to more parsimonious pattern matches. Often the way to avoid a pattern is to use the complement operator and a character class. Look at the example, and think about how it works. The trick here is that there are two different ways of formulating almost the same sequence. For people who have thought about basic probability, the same pattern occurs.

How to check whether JavaScript is enabled in client browser using Java code Clojure’s new regex syntax Last week, Rich Hickey announced a few notable changes to Clojure, including ahead-of-time compilation and a cleaner syntax for regular expressions. Both are improvements, but the syntax is especially interesting for a reason unrelated to its function. First, a quick overview. 1. What has changed In a sentence, fewer backslashes. Say we are given a stream including this text: ... We want to select IMG tags and capture the basename (without extension) of each source file. <img [whitespace]+ src=" [word-char]+ / [digit]+ / ([word-char]+) ... Converting this to Clojure’s old syntax gives us a somewhat unwieldly #"<img\\s+src=\"\\w+/\\d+/(\\w+)". (let [lines "... The new update to the reader allows us to remove the double escaping of the regex specials in the literal: (map second (re-seq #"<img\s+src=\"\w+/\d+/(\w+)" lines))) 2. Since we’re on the topic, here’s how Clojure’s syntax compares to popular languages. Ruby and Perl 5 Emacs Lisp "<img\\s-+src=\"\\w+/[[:digit:]]+/\\(\\w+\\)" Java Common Lisp

Javascript Function to Check or Uncheck all Checkboxes This Javascript function will check or uncheck all the checkboxes in an HTML form. This function is specially designed for dynamic pages with varying numbers of checkboxes. Unlike other functions out there, it will work without error even if there are no checkboxes or there is only one checkbox. It is also significantly faster for long lists, because it saves the length of the checkbox in a variable, instead of recalculating it in the loop. Finally, the function is granted to the public domain--use it as you wish. Instructions Provide the form name and the field name of the checkbox as the parameters to the function. JavaScript Source Code function SetAllCheckBoxes(FormName, FieldName, CheckValue) { if(! Example Use your browser's view source command to see how the buttons below work to select or unselect all the checkboxes. Created 2004-12-13, Last Modified 2011-07-24, © Shailesh N.

Regular Expressions: Now You Have Two Problems I love regular expressions. No, I'm not sure you understand: I really love regular expressions. You may find it a little odd that a hack who grew up using a language with the ain't keyword would fall so head over heels in love with something as obtuse and arcane as regular expressions. I'm not sure how that works. But it does. If you've ever talked about regular expressions with another programmer, you've invariably heard this 1997 chestnut: Some people, when confronted with a problem, think "I know, I'll use regular expressions." The quote is from Jamie Zawinski, a world class hacker who I admire greatly. Perl's nature encourages the use of regular expressions almost to the exclusion of all other techniques; they are far and away the most "obvious" (at least, to people who don't know any better) way to get from point A to point B. The first quote is too glib to be taken seriously. I couldn't agree more. But wait! Calm down. Look. Buck up, soldier. var whitelist = @"</?

The implementation of Factor&#039;s regexp library I've been working on Factor's regular expression library, initially written by Doug Coleman, for the past few weeks. Recently, the library became good enough that I've pushed it to Factor's main repository. The latest Factor binaries have this new library. The library uses an standard algorithm of converting a regular expression into an NFA, and that into a DFA which can be executed. The main features missing now are Possessive and reluctant matchingGroup captureUnicode support, in the form of UTS 18 level 1 compliance with some level 2 featuresRight now, I'm working on Unicode support. The rest of this article is an overview of how the regexp engine works. The parser The parser is implemented with Chris Double's packrat parsing library. If I were working only with ASCII, then ranges would be expanded into disjunctions as well, but the alphabet is far too big for that. Constructing an NFA From the syntax tree, a nondeterministic finite-state automaton is built. Disambiguation Minimization

SLRE - Super Light Regular Expression library Parsing Strings With jQuery : DevKick Blog Regular Expressions for Regular Programmers Parse - cruiser: js parser generator When you reach the limits of regular expressions in JavaScript, you have two choices: 1. Realize that what you are trying to do is probably not a good idea. 2. Write yourself a parser generator and keep on going. The addition of CSS 3 selector support broke our regex-based parser for Behaviors. What this library gives you is the ability to write parsers very easily. Here's the grammar for CSS (probably not perfect, but good enough for our purposes): Most of it is pretty self-explanatory. Functions like pair and between give the parser some additional intelligence beyond just sequences of tokens and alternate paths. Each of the functions described below return a function that will match based on the given criteria. token( token ). any( rule1, rule2, ... ). each( rule1, rule2, ...). many( rule ). list( rule, delim, trailing ). between( rdelim, rule, ldelim ). pair( rule1, rule2, delim ). process( rule, fn ). ignore( rule). You can easily add additional operators.

A Regular Expression Test Applet The point of this applet is to provide a convenient means of testing regular expressions before embedding them in a Java program. The buttons represent calls to the Java methods with the same name. The applet executes (approximately) the following code: pattern = Pattern.compile(Contents of the Pattern: field); matcher = pattern.matcher(Contents of the String: field); matcher.Whatever_method_you_clicked(); Display results; The In Java: field shows how you would have to write the regular expression as a Java literal string. For successful matches, the Results: area will show what values you will get if you call the methods start(), end(), and group(n). For those who are interested, here's the source code.

Related: