Skip to content

Commit 476bd69

Browse files
committed
Added first version and readme file
1 parent 792f364 commit 476bd69

File tree

2 files changed

+200
-3
lines changed

2 files changed

+200
-3
lines changed

README.md

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,71 @@
1-
VerbalExpressions
2-
=================
1+
VerbalExpressions v0.1
2+
=====================
33

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

VerbalExpressions.js

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*!
2+
* VerbalExpressions JavaScript Library v0.1
3+
* https://github.com/jehna/VerbalExpressions
4+
*
5+
*
6+
* Released under the MIT license
7+
* http://jquery.org/license
8+
*
9+
* Date: 2013-07-19
10+
*
11+
*/
12+
13+
// Define the collection class.
14+
window.VerbalExpression = (function(){
15+
16+
// I am the constructor function.
17+
function VerbalExpression(){
18+
var verbalExpression = Object.create( RegExp.prototype );
19+
20+
// Initialize
21+
verbalExpression = (RegExp.apply( verbalExpression, arguments ) || verbalExpression);
22+
23+
// Add all the class methods
24+
VerbalExpression.injectClassMethods( verbalExpression );
25+
26+
// Return the new object.
27+
return( verbalExpression );
28+
}
29+
30+
31+
// Define the static methods.
32+
VerbalExpression.injectClassMethods = function( verbalExpression ){
33+
34+
// Loop over all the prototype methods
35+
for (var method in VerbalExpression.prototype){
36+
37+
// Make sure this is a local method.
38+
if (VerbalExpression.prototype.hasOwnProperty( method )){
39+
40+
// Add the method
41+
verbalExpression[ method ] = VerbalExpression.prototype[ method ];
42+
43+
}
44+
45+
}
46+
47+
return( verbalExpression );
48+
49+
};
50+
51+
52+
// Define the class methods.
53+
VerbalExpression.prototype = {
54+
55+
_source : "",
56+
57+
lol: function( value ){
58+
console.log( "loL" );
59+
this.compile( value );
60+
return( this );
61+
},
62+
63+
sanitize : function( value ) {
64+
return value.replace(/[^\w]/g, function(character) { return "\\" + character; });
65+
},
66+
67+
startOfLine: function() {
68+
if(this.source.indexOf( "^" ) !== 0) this._source = "^" + this._source;
69+
this.add( "" );
70+
return( this );
71+
},
72+
73+
add: function( value ) {
74+
this._source += value || "";
75+
this.compile(this._source);
76+
return( this );
77+
},
78+
79+
then : function( value ) {
80+
value = this.sanitize(value);
81+
this.add( "(" + value + ")" );
82+
return( this );
83+
},
84+
85+
find : function( value ) {
86+
return( this.then( value ) );
87+
},
88+
89+
maybe : function( value ) {
90+
value = this.sanitize(value);
91+
this.add( "(" + value + ")?" );
92+
return( this );
93+
},
94+
95+
anything : function() {
96+
this.add( "(.*)" );
97+
return( this );
98+
},
99+
100+
endOfLine : function() {
101+
this.add( "$" );
102+
return( this );
103+
},
104+
105+
anythingBut : function( value ) {
106+
value = this.sanitize(value);
107+
this.add( "([^" + value + "]*)" );
108+
return( this );
109+
},
110+
111+
replace : function( source, value ) {
112+
source = source.toString();
113+
return source.replace( this, value );
114+
}
115+
116+
};
117+
118+
119+
// Return the constructor.
120+
return( VerbalExpression );
121+
122+
123+
}).call( {} );
124+
125+
// Create shorthand
126+
(function(w) {
127+
w.VerEx = function() {
128+
return new VerbalExpression();
129+
};
130+
})(window);

0 commit comments

Comments
 (0)