|
1 |
| -##PHPVerbalExpressions |
2 |
| -- ported from [VerbalExpressions](https://github.com/jehna/VerbalExpressions) |
| 1 | +VerbalExpressions v0.1 |
| 2 | +===================== |
3 | 3 |
|
4 |
| -VerbalExpressions is a PHP library that helps to construct hard regular expressions. |
| 4 | +## JavaScript Regular Expressions made easy |
| 5 | +VerbalExpressions is a JavaScript library that helps to construct difficult regular expressions. |
5 | 6 |
|
| 7 | +## How to get started |
6 | 8 |
|
7 |
| -```php |
| 9 | +Include the library and you're good to go! |
| 10 | +```HTML |
| 11 | +<script src="VerbalExpressions.js"></script> |
| 12 | +``` |
| 13 | + |
| 14 | +## Examples |
8 | 15 |
|
9 |
| -// some tests |
| 16 | +Here's a couple of simple examples to give an idea of how VerbalExpressions works: |
10 | 17 |
|
11 |
| -$regex = new VerEx; |
| 18 | +### Testing if we have a valid URL |
12 | 19 |
|
13 |
| -$regex ->startOfLine() |
14 |
| - ->then( "http" ) |
15 |
| - ->maybe( "s" ) |
16 |
| - ->then( "://" ) |
17 |
| - ->maybe( "www." ) |
18 |
| - ->anythingBut( " " ) |
19 |
| - ->endOfLine(); |
| 20 | +```javascript |
| 21 | +// Create an example of how to test for correctly formed URLs |
| 22 | +var tester = VerEx() |
| 23 | + .startOfLine() |
| 24 | + .then( "http" ) |
| 25 | + .maybe( "s" ) |
| 26 | + .then( "://" ) |
| 27 | + .maybe( "www." ) |
| 28 | + .anythingBut( " " ) |
| 29 | + .endOfLine(); |
20 | 30 |
|
| 31 | +// Create an example URL |
| 32 | +var testMe = "https://www.google.com"; |
21 | 33 |
|
22 |
| -if($regex->test("http://github.com")) |
23 |
| - echo "valid url"; |
24 |
| -else |
25 |
| - echo "invalid url"; |
| 34 | +// Use RegExp object's native test() function |
| 35 | +if( tester.test( testMe ) ) alert( "We have a correct URL "); // This output will fire |
| 36 | +else alert( "The URL is incorrect" ); |
| 37 | + |
| 38 | +console.log( tester ); // Ouputs the actual expression used: /^(http)(s)?(\:\/\/)(www\.)?([^\ ]*)$/ |
| 39 | +``` |
26 | 40 |
|
| 41 | +### Replacing strings |
27 | 42 |
|
28 |
| -echo "<pre>". $regex->getRegex() ."</pre>"; |
| 43 | +```javascript |
| 44 | +// Create a test string |
| 45 | +var replaceMe = "Replace bird with a duck"; |
29 | 46 |
|
| 47 | +// Create an expression that seeks for word "bird" |
| 48 | +var expression = VerEx().find( "bird" ); |
| 49 | + |
| 50 | +// Execute the expression like a normal RegExp object |
| 51 | +var result = replaceMe.replace( expression, "duck" ); |
| 52 | + |
| 53 | +alert( result ); // Outputs "Replace duck with a duck" |
| 54 | +``` |
30 | 55 |
|
31 |
| -echo $regex ->clean(array("modifiers"=> "m","replaceLimit"=>4)) |
32 |
| - ->find(' ') |
33 |
| - ->replace("This is a small test http://somesite.com and some more text.", "-"); |
| 56 | +### Shorthand for string replace: |
34 | 57 |
|
| 58 | +```javascript |
| 59 | +var result = VerEx().find( "red" ).replace( "We have a red house", "blue" ); |
| 60 | +alert( result ); // Outputs "We have a blue house" |
35 | 61 | ```
|
| 62 | + |
| 63 | +## API documentation |
| 64 | + |
| 65 | +You can find the API documentation at the [wiki pages](https://github.com/jehna/VerbalExpressions/wiki). |
| 66 | + |
| 67 | +## A little word for a big help |
| 68 | +I'd like to promote a special thank-you to [Ben Nadel][ben-nadel] for his [great article about extending native JS objects][extending] |
| 69 | + |
| 70 | +## Contributions |
| 71 | +Clone the repo and fork: |
| 72 | +`git clone https://github.com/jehna/VerbalExpressions.git`. |
| 73 | + |
| 74 | +Pull requests are warmly welcome! |
| 75 | + |
| 76 | +[ben-nadel]:http://www.bennadel.com/ |
| 77 | +[extending]:http://www.bennadel.com/blog/2292-Extending-JavaScript-Arrays-While-Keeping-Native-Bracket-Notation-Functionality.htm |
0 commit comments