Advertisement
Gordon___From

JS regexp

Feb 23rd, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /pattern/[switches]
  2.  
  3. 1. Meta chars vs literals
  4. 2. Character classes [..]
  5.     [abc]   Character class, any character from a set of characters
  6.     [^abc]  Inverted char class, any character not in a set of characters
  7.     [0-9] or [a-zA-Z] Any character in a range of characters
  8. 3. Quantifiers:
  9.     x+  One or more occurrences of the pattern x
  10.     x?  Zero or one occurrence
  11.     x*  Zero or more occurrences
  12.     x+? One or more occurrences, non-greedy
  13.     x{n}    Interval quantifier, matches exactly n times
  14.     x{n,}   At least n times
  15.     x{n,m}  Between n and m occurrences
  16.  
  17. 4. backreferencing
  18.     \n - A back reference to the last substring matching the n parenthetical
  19.          in the regular expression (counting left parentheses).
  20.     For example, /apple(,)\sorange\1/ matches 'apple, orange,' in "apple, orange, cherry, peach."
  21.  
  22. .   Any single character except newlines
  23. abc A sequence of characters
  24. (abc)   A group
  25. a|b|c   Any one of several patterns
  26. \d  Any digit character
  27. \w  An alphanumeric character (“word character”)
  28. \s  Any whitespace character
  29. \b  A word boundary
  30. ^   Start of input
  31. $   End of input
  32. ^$  line without chars
  33.  
  34. 1. BACKREFFERENCING: Find word duplicates - /\b(\w+)\s+\1\b/
  35. This This helper validates attributes against a block. It doesn't have a predefined validation function. You should create one using a block, and every attribute passed to validates_each will be tested against it it. In the following example, we don't want names and surnames to begin begin with lower case.
  36.  
  37. 2. HTML: find target="_blank" attribute duplicate in html-php code - /<a.+?target="_blank".+?target="_blank".+?[^?]>/
  38. <a target="_blank" style="font-family:arial,sans-serif;font-size:15px;line-height:21px;color:#5f230a;text-decoration:none;" href="<?= $highlight['NewsLetter']['url'] ?>" target="_blank">
  39.  
  40. 3. Email validation
  41. 4. BASICS::CHAR_CLASSES : find all word that has foo in the middle
  42. 5. BASICS::CHAR_CLASSES : regex that matches dates like 19.06.2000
  43. 6. Word in quotes /"[^"]*"/
  44. 7. Find price in $12.33 like in text: /\$[0-9]+(\.[0-9]+)?/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement