JSVerbalExpressions README

The project's README from github.com/VerbalExpressions/JSVerbalExpressions. As one markdown file: README.md.

VerbalExpressions

Build Status Latest Version jsDelivr License

JavaScript Regular Expressions made easy

VerbalExpressions is a JavaScript library that helps construct difficult regular expressions.

How to get started

In the browser

<script src="VerbalExpressions.js"></script>

Or use the jsDelivr CDN.

On the server (node.js)

Install:

npm install verbal-expressions

Require:

const VerEx = require('verbal-expressions');

Or use ES6's import:

import VerEx from 'verbal-expressions';

Running tests

npm test

(or)

npm run test:verbose

Creating a minified version

npm run build

This will run Babel on VerbalExpressions.js and output the result to dist/verbalexpressions.js. A minified version of the same will also be written to dist/verbalexpressions.min.js.

A source map will also be created in dist, so you can use the original "un-babelified", unminified source file for debugging purposes.

Building the docs/ folder

The docs/ folder uses Jekyll for building the static HTML and is hosted at gh-pages.

To install the Ruby dependencies, run:

cd docs/
bundle install

This installs all needed Ruby dependencies locally

After you've installed dependencies, you can run:

bundle exec jekyll build

This builds all static files to docs/_site/ folder.

If you want to develop the files locally, you can run:

bundle exec jekyll serve

This starts a local development web server and starts watching your files for changes.

API documentation

You can find the API documentation at verbalexpressions.github.io/JSVerbalExpressions. You can find the source code for the docs in docs.

Examples

Here are some simple examples to give an idea of how VerbalExpressions works:

Testing if we have a valid URL

// Create an example of how to test for correctly formed URLs
const tester = VerEx()
    .startOfLine()
    .then('http')
    .maybe('s')
    .then('://')
    .maybe('www.')
    .anythingBut(' ')
    .endOfLine();

// Create an example URL
const testMe = 'https://www.google.com';

// Use RegExp object's native test() function
if (tester.test(testMe)) {
    alert('We have a correct URL'); // This output will fire
} else {
    alert('The URL is incorrect');
}

console.log(tester); // Outputs the actual expression used: /^(http)(s)?(\:\/\/)(www\.)?([^\ ]*)$/

Replacing strings

// Create a test string
const replaceMe = 'Replace bird with a duck';

// Create an expression that seeks for word "bird"
const expression = VerEx().find('bird');

// Execute the expression like a normal RegExp object
const result = expression.replace(replaceMe, 'duck');

// Outputs "Replace duck with a duck"
alert(result);

Shorthand for string replace

const result = VerEx().find('red').replace('We have a red house', 'blue');

// Outputs "We have a blue house"
alert(result);

Contributions

Pull requests are warmly welcome!

Clone the repo and fork:

git clone https://github.com/VerbalExpressions/JSVerbalExpressions.git

Style guide

The Airbnb style guide is loosely used as a basis for creating clean and readable JavaScript code. Check .eslintrc.

Check out these slide decks for handy Github & git tips:

Tools

Other Implementations

You can see an up to date list of all ports on VerbalExpressions.github.io.

If you would like to contribute another port (which would be awesome!), please open an issue specifying the language in the VerbalExpressions/implementation repo. Please don't open PRs for other languages against this repo.

Similar projects

Here's a list of other similar projects that implement regular expression builders:

JSVerbalExpressions (VerbalExpressions/JSVerbalExpressions)

Searching Database…

这是什么?VerbalExpressions 是一个 JavaScript 库,让你可以用可链式调用、易读的方法来构建正则表达式,而不必直接编写原始的正则语法。
它是框架还是库?它是一个小型库:你在 VerEx 对象上调用 startOfLine()、then()、endOfLine() 等方法,底层会生成一个真正的 RegExp。
我该如何把它添加到项目中?在浏览器中,可以直接加载该文件,或通过 jsDelivr CDN 加载;在 Node.js 中,先安装,再用 require 引入,或使用 ES6 的 import 语法导入。
我只想直接使用它,需要任何构建工具吗?不需要,你可以直接使用预构建好的库;只有当你想运行测试,或从源码生成自己的压缩版本时,才需要构建工具(Grunt/Babel)。
这是否也是其他语言唯一的正则构建器?不是,JSVerbalExpressions 是 JavaScript 版本;Ruby、C#、Python、Java、Groovy、PHP、Haskell、Haxe、C++、Objective-C、Perl 和 Swift 各自都有独立的实现仓库,由社区维护。
能举例说明链式语法是什么样子的吗?README 中的示例展示了如何构建一个 URL 验证器,以及如何进行字符串替换:通过链式调用 startOfLine()、then('http')、maybe('s')、then('://') 等方法,最后得到可用的 RegExp 或调用 replace()。
有没有无需安装就能试用的在线工具?有的,README 中提供了一个 JSBin Playground 工具的链接,可以在浏览器中交互式地体验 VerbalExpressions。
我能用普通文字写正则,然后得到 VerbalExpressions 代码吗?README 中提到了一个配套工具,它对 JSVerbalExpressions 进行了封装,让用户可以用普通代码编写,再借助该库编译成正则表达式。
文档网站是如何构建的?docs/ 文件夹使用 Jekyll 生成静态 HTML,并托管在 GitHub Pages 上,地址为 verbalexpressions.github.io/JSVerbalExpressions。
What are similar projects?lodash, core-js, validator.js, classnames, co, mojs, dropzone, pug
Size
2.8 MB
Stars
12.1K
Age
13y
Commits · 90 Days