java parse是什么,让我们一起了解一下?
Parse是一个使用语法规则来解析输入序列的内部DSL(在Rebol生态圈称为“方言”)。Parse方言是TDPL家族的突出一员,常用来校验,验证,分解,修改输入的数据,甚至是实现内部或者外部DSL。
Parse的规则是由哪些元素构成的?
关键字:Parse方言预留的单词。
单字(word):单字所绑定的值被用于规则。
设字(word:):将单字绑定到当前的输入流位置。
取字(:word):恢复单字绑定的输入流位置。
整型数值:指定规则重复的数量或者范围。
字面值:匹配输入流中对应的字面值。
[rules]:子规则区块。
(expression):脱离Parse方言转而执行Red表达式,执行完毕后返回到Parse方言。
Parse的方法是如何实现的?
示例代码如下:
const path = require("path");
const url=require("url");
let str="/images/fff/123/jj.jpg";
console.log(path.parse(str));
结果:
{
root: '/',
dir: '/images/fff/123',
base: 'jj.jpg',
ext: '.jpg',
name: 'jj'
}
console.log(path.sep);// \
let u = "//www.how234.com:8080/images/fff/123/jj.jpg?id=1&name=tom#hash";
console.log(url.parse(u));//query
结果:
\
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: '#hash',
search: '?id=1&name=tom',
query: 'id=1&name=tom',
pathname: '//www.how234.com:8080/images/fff/123/jj.jpg',//pathname 属性是一个可读可写的字符串,可设置或返回当前 URL 的路径部分
path: '//www.how234.com:8080/images/fff/123/jj.jpg?id=1&name=tom',
href: '//www.how234.com:8080/images/fff/123/jj.jpg?id=1&name=tom#hash'
}
console.log(url.parse(u,true));
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: '#hash',
search: '?id=1&name=tom',
query: [Object: null prototype] { id: '1', name: 'tom' },//第二个参数为true,query属性就会从查询字符串格式(“a=1&b=2”)转换为了对象格式({a: 1,b: 2})
pathname: '//www.how234.com:8080/images/fff/123/jj.jpg',
path: '//www.how234.com:8080/images/fff/123/jj.jpg?id=1&name=tom',
href: '//www.how234.com:8080/images/fff/123/jj.jpg?id=1&name=tom#hash'
}
console.log(url.parse(u, true, true));
Url {
protocol: null,
slashes: true,
auth: null,
host: 'www.how234.com:8080',//host
port: '8080',
hostname: 'www.how234.com',
hash: '#hash',
search: '?id=1&name=tom',
query: [Object: null prototype] { id: '1', name: 'tom' },
pathname: '/images/fff/123/jj.jpg',
path: '/images/fff/123/jj.jpg?id=1&name=tom',
href: '//www.how234.com:8080/images/fff/123/jj.jpg?id=1&name=tom#hash'
}以上就是小编今天的分享了,希望可以帮助到大家。