Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jun 9, 2025. It is now read-only.

Commitbe13927

Browse files
authored
Merge pull request#5 from SimpleRegex/dev
Release 0.2.0 to follow SRL standard
2 parents9c19736 +ca38519 commitbe13927

File tree

8 files changed

+189
-124
lines changed

8 files changed

+189
-124
lines changed

‎README.md‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ JavaScript implementation of [Simple Regex](https://simple-regex.com/) :tada::ta
99
>Because of the JavaScript regex engine, there is something different from[Simple Regex](https://simple-regex.com/)
1010
- NOT support`as` to assign capture name.
1111
- NOT support`if already had/if not already had`
12-
- NO`firstMatch`, since in JavaScript`lazy` means non-greedy (matching the fewest possible characters).
12+
- NO`first match` and NO`all lazy`, since in JavaScript`lazy` means non-greedy (matching the fewest possible characters).
1313

1414
##Installation
1515

@@ -26,21 +26,29 @@ The builder can agent `test/exec` method to the generated regex object. Or you c
2626
```js
2727
constSRL=require('srl')
2828
constquery=newSRL('letter exactly 3 times')
29-
constregex=query.get()// /[a-z]{3}/
3029

31-
query.test('aaa')// true
32-
query.exec('aaa')// [ 'aaa', index: 0, input: 'aaa' ]
30+
query.isMatching('aaa')// true
31+
query.getMatch('aaa')// [ 'aaa', index: 0, input: 'aaa' ]
3332

3433
query
3534
.digit()
3635
.neverOrMore()
3736
.mustEnd()
38-
.get()// /[a-z]{3}[0-9]*$/
37+
.get()// /[a-z]{3}[0-9]*$/g
3938
```
4039

4140
Required Node 6.0+ for the ES6 support, Or you can use[Babel](http://babeljs.io/) to support Node below 6.0.
4241

43-
Using[Webpack](http://webpack.github.io) and[babel-loader](https://github.com/babel/babel-loader) to pack it if want to use in browsers.
42+
Using[Webpack](http://webpack.github.io) and[babel-loader](https://github.com/babel/babel-loader) to pack it if want to use in browsers.
43+
44+
##Additional
45+
46+
In SRL-JavaScript we apply`g` flag as default to follow the[Simple Regex](https://simple-regex.com/) "standard", so we provide more API to use regex conveniently.
47+
48+
-`isMatching` - Validate if the expression matches the given string.
49+
-`getMatch` - Get first match of the given string, like run`regex.exec` once.
50+
-`getMatches` - Get all matches of the given string, like a loop to run`regex.exec`.
51+
-`removeModifier` - Remove specific flag.
4452

4553
##Development
4654

‎lib/Builder.js‎

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class Builder {
8080
this._regEx=[]
8181

8282
/**@var {string} _modifiers Raw modifier to apply on. */
83-
this._modifiers=''
83+
this._modifiers='g'
8484

8585
/**@var {number} _lastMethodType Type of last method, to avoid invalid builds. */
8686
this._lastMethodType=METHOD_TYPE_BEGIN
@@ -379,10 +379,6 @@ class Builder {
379379
/* MODIFIER MAPPER */
380380
/**********************************************************/
381381

382-
all(){
383-
returnthis._addUniqueModifier('g')
384-
}
385-
386382
multiLine(){
387383
returnthis._addUniqueModifier('m')
388384
}
@@ -486,6 +482,7 @@ class Builder {
486482
this._regEx.push(condition)
487483
returnthis
488484
}
485+
489486
/**
490487
* Validate method call. This will throw an exception if the called method makes no sense at this point.
491488
* Will add the current type as the last method type.
@@ -619,6 +616,8 @@ class Builder {
619616
}
620617

621618
/**
619+
* Clone a new builder object.
620+
*
622621
*@return {Builder}
623622
*/
624623
clone(){
@@ -633,6 +632,19 @@ class Builder {
633632
returnclone
634633
}
635634

635+
/**
636+
* Remote specific flag.
637+
*
638+
*@param {string} flag
639+
*@return {Builder}
640+
*/
641+
removeModifier(flag){
642+
this._modifiers=this._modifiers.replace(flag,'')
643+
this._result=null
644+
645+
returnthis
646+
}
647+
636648
/**********************************************************/
637649
/* REGEX METHODS */
638650
/**********************************************************/
@@ -645,6 +657,52 @@ class Builder {
645657
constregexp=this.get()
646658
returnregexp.test.apply(regexp,arguments)
647659
}
660+
661+
/**********************************************************/
662+
/* ADDITIONAL METHODS */
663+
/**********************************************************/
664+
665+
/**
666+
* Just like test in RegExp, but reset lastIndex.
667+
*
668+
*@param {string} target
669+
*@return {boolean}
670+
*/
671+
isMatching(target){
672+
constresult=this.test(target)
673+
this.get().lastIndex=0
674+
returnresult
675+
}
676+
677+
/**
678+
* Just like match in String, but reset lastIndex.
679+
*
680+
*@param {string} target
681+
*@return {array|null}
682+
*/
683+
getMatch(target){
684+
constregex=this.get()
685+
constresult=regex.exec(target)
686+
regex.lastIndex=0
687+
returnresult
688+
}
689+
690+
/**
691+
* Get all matches, just like loop for RegExp.exec.
692+
*@param {string} target
693+
*/
694+
getMatches(target){
695+
constresult=[]
696+
constregex=this.get()
697+
lettemp=null
698+
699+
while(temp=regex.exec(target)){
700+
result.push(temp)
701+
}
702+
regex.lastIndex=0
703+
704+
returnresult
705+
}
648706
}
649707

650708
module.exports=Builder

‎lib/Language/Helpers/methodMatch.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ const mapper = {
2323
'new line':{'class':SimpleMethod,'method':'newLine'},
2424
'whitespace':{'class':SimpleMethod,'method':'whitespace'},
2525
'no whitespace':{'class':SimpleMethod,'method':'noWhitespace'},
26-
'all':{'class':SimpleMethod,'method':'all'},
2726
'anything':{'class':SimpleMethod,'method':'any'},
2827
'tab':{'class':SimpleMethod,'method':'atb'},
2928
'digit':{'class':SimpleMethod,'method':'digit'},

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name":"srl",
3-
"version":"0.1.0",
3+
"version":"0.2.0",
44
"description":"Simple Regex Language",
55
"main":"lib/SRL.js",
66
"scripts": {

‎test/builder-test.js‎

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict'
22

33
constassert=require('assert')
4-
constSRL=require('../lib/Builder')
4+
constSRL=require('../')
55

6-
describe('BuilderTest',()=>{
6+
describe('BuilderisMatching',()=>{
77
it('Simple Phone Number Format',()=>{
88
constregex=newSRL()
99
.startsWith()
@@ -15,12 +15,12 @@ describe('Builder Test', () => {
1515
.digit().onceOrMore()
1616
.mustEnd()
1717

18-
assert.ok(regex.test('+49 123-45'))
19-
assert.ok(regex.exec('+492 1235-4'))
20-
assert.ok(!regex.test('+49 123 45'))
21-
assert.ok(!regex.exec('49 123-45'))
22-
assert.ok(!regex.test('a+49 123-45'))
23-
assert.ok(!regex.test('+49 123-45b'))
18+
assert.ok(regex.isMatching('+49 123-45'))
19+
assert.ok(regex.isMatching('+492 1235-4'))
20+
assert.ok(!regex.isMatching('+49 123 45'))
21+
assert.ok(!regex.isMatching('49 123-45'))
22+
assert.ok(!regex.isMatching('a+49 123-45'))
23+
assert.ok(!regex.isMatching('+49 123-45b'))
2424
})
2525

2626
it('Simple Email Format',()=>{
@@ -39,15 +39,14 @@ describe('Builder Test', () => {
3939
.letter().atLeast(2)
4040
.mustEnd()
4141
.caseInsensitive()
42-
.get()// Use get() to test resulting RegExp object.
43-
44-
assert.equal('sample@example.com'.match(regex)[0],'sample@example.com')
45-
assert.equal(regex.exec('super-He4vy.add+ress@top-Le.ve1.domains'),'super-He4vy.add+ress@top-Le.ve1.domains')
46-
assert.ok(!regex.test('sample.example.com'))
47-
assert.ok(!regex.test('missing@tld'))
48-
assert.ok(!regex.test('hav ing@spac.es'))
49-
assert.ok(!regex.test('no@pe.123'))
50-
assert.ok(!regex.test('invalid@email.com123'))
42+
43+
assert.equal(regex.getMatch('sample@example.com')[0],'sample@example.com')
44+
assert.equal(regex.getMatch('super-He4vy.add+ress@top-Le.ve1.domains')[0],'super-He4vy.add+ress@top-Le.ve1.domains')
45+
assert.ok(!regex.isMatching('sample.example.com'))
46+
assert.ok(!regex.isMatching('missing@tld'))
47+
assert.ok(!regex.isMatching('hav ing@spac.es'))
48+
assert.ok(!regex.isMatching('no@pe.123'))
49+
assert.ok(!regex.isMatching('invalid@email.com123'))
5150
})
5251

5352
it('Capture Group',()=>{
@@ -65,14 +64,13 @@ describe('Builder Test', () => {
6564
query.letter().onceOrMore()
6665
})
6766
.literally('.')
68-
.get()
6967

70-
assert.ok(regex.test('my favorite color: blue.'))
71-
assert.ok(regex.test('my favorite colour is green.'))
72-
assert.ok(!regex.test('my favorite colour is green!'))
68+
assert.ok(regex.isMatching('my favorite color: blue.'))
69+
assert.ok(regex.isMatching('my favorite colour is green.'))
70+
assert.ok(!regex.isMatching('my favorite colour is green!'))
7371

7472
consttestcase='my favorite colour is green. And my favorite color: yellow.'
75-
constmatches=testcase.match(regex)
73+
constmatches=regex.getMatch(testcase)
7674
assert.equal(matches[1],'green')
7775
})
7876

@@ -86,12 +84,12 @@ describe('Builder Test', () => {
8684
.tab()
8785
.mustEnd()
8886
.multiLine()
89-
.get()
87+
9088
consttarget=`
9189
ba\t
9290
aaabbb
9391
`
94-
assert.ok(regex.test(target))
92+
assert.ok(regex.isMatching(target))
9593

9694
constregex2=newSRL()
9795
.startsWith()
@@ -101,10 +99,10 @@ describe('Builder Test', () => {
10199
.onceOrMore()
102100
.literally('b')
103101
.mustEnd()
104-
.get()
102+
105103
consttarget2=`a
106104
b`
107-
assert.ok(regex2.test(target2))
105+
assert.ok(regex2.isMatching(target2))
108106
})
109107

110108
it('Replace',()=>{
@@ -133,28 +131,25 @@ describe('Builder Test', () => {
133131
.whitespace().optional()
134132
.lazy()
135133
})
136-
.get()
137134

138-
constmatches=',, '.match(regex)
135+
constmatches=regex.getMatch(',, ')
139136
assert.equal(matches[1],',,')
140137
assert.notEqual(matches[1],',, ')
141138

142139
constregex2=newSRL()
143140
.literally(',')
144141
.atLeast(1)
145142
.lazy()
146-
.get()
147143

148-
constmatches2=regex2.exec(',,,,,')
144+
constmatches2=regex2.getMatch(',,,,,')
149145
assert.equal(matches2[0],',')
150146
assert.notEqual(matches2[0],',,,,,')
151147

152148
})
153149

154-
it('Global',()=>{
150+
it('Global as Default',()=>{
155151
constregex=newSRL()
156152
.literally('a')
157-
.all()
158153
.get()
159154

160155
letcount=0
@@ -169,9 +164,18 @@ describe('Builder Test', () => {
169164
.raw('b[a-z]r')
170165
.raw(/\d+/)
171166

172-
assert.ok(regex.test('foobzr123'))
173-
assert.ok(regex.test('foobar1'))
174-
assert.ok(!regex.test('fooa'))
175-
assert.ok(!regex.test('foobar'))
167+
assert.ok(regex.isMatching('foobzr123'))
168+
assert.ok(regex.isMatching('foobar1'))
169+
assert.ok(!regex.isMatching('fooa'))
170+
assert.ok(!regex.isMatching('foobar'))
171+
})
172+
173+
it('Remove modifier',()=>{
174+
constregex=newSRL()
175+
.literally('foo')
176+
.removeModifier('g')
177+
.get()
178+
179+
assert.deepEqual(regex,/(?:foo)/)
176180
})
177181
})

‎test/cache-test.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('Cache', () => {
1212
})
1313

1414
it('In interpreter',()=>{
15-
constRE=/(?:a)/
15+
constRE=/(?:a)/g
1616
constquery=newInterpreter('Literally "a"')
1717
assert.deepEqual(query.get(),RE)
1818

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp