regexp
正则表达式工具。
导入
typescript
import {
regexpEscape,
isEmail,
isPhone,
isIDNo,
isURL,
isIPV4,
isInteger,
isFloat,
isNumerical,
isDigit
} from '@cloudcome/utils-core/regexp'函数
regexpEscape
转义正则表达式特殊字符。
typescript
function regexpEscape(string: string): string参数
| 参数 | 类型 | 描述 |
|---|---|---|
| string | string | 要转义的字符串 |
返回值
string - 转义后的字符串
示例
typescript
regexpEscape('hello.world') // 'hello\\.world'
regexpEscape('$100') // '\\$100'
regexpEscape('(test)') // '\\(test\\)'isEmail
判断是否为有效的邮箱地址。
typescript
function isEmail(value: string): boolean参数
| 参数 | 类型 | 描述 |
|---|---|---|
| value | string | 要检查的字符串 |
返回值
boolean - 是否为有效的邮箱地址
示例
typescript
isEmail('user@example.com') // true
isEmail('user.name+tag@example.com') // true
isEmail('user@') // false
isEmail('@example.com') // falseisPhone
判断是否为有效的手机号码(中国大陆)。
typescript
function isPhone(value: string): boolean参数
| 参数 | 类型 | 描述 |
|---|---|---|
| value | string | 要检查的字符串 |
返回值
boolean - 是否为有效的手机号码
示例
typescript
isPhone('13800138000') // true
isPhone('15912345678') // true
isPhone('12345678901') // false
isPhone('1380013800') // falseisIDNo
判断是否为有效的身份证号码(中国大陆)。
typescript
function isIDNo(value: string): boolean参数
| 参数 | 类型 | 描述 |
|---|---|---|
| value | string | 要检查的字符串 |
返回值
boolean - 是否为有效的身份证号码
示例
typescript
isIDNo('110101199003074518') // true(18 位)
isIDNo('110101990307451') // true(15 位)
isIDNo('123456789012345') // falseisURL
判断是否为有效的 URL。
typescript
function isURL(value: string): boolean参数
| 参数 | 类型 | 描述 |
|---|---|---|
| value | string | 要检查的字符串 |
返回值
boolean - 是否为有效的 URL
示例
typescript
isURL('https://example.com') // true
isURL('http://example.com/path?query=1') // true
isURL('ftp://example.com') // true
isURL('example.com') // falseisIPV4
判断是否为有效的 IPv4 地址。
typescript
function isIPV4(value: string): boolean参数
| 参数 | 类型 | 描述 |
|---|---|---|
| value | string | 要检查的字符串 |
返回值
boolean - 是否为有效的 IPv4 地址
示例
typescript
isIPV4('192.168.1.1') // true
isIPV4('10.0.0.1') // true
isIPV4('256.0.0.1') // false
isIPV4('192.168.1') // falseisInteger
判断是否为整数字符串。
typescript
function isInteger(value: string): boolean参数
| 参数 | 类型 | 描述 |
|---|---|---|
| value | string | 要检查的字符串 |
返回值
boolean - 是否为整数字符串
示例
typescript
isInteger('123') // true
isInteger('-123') // true
isInteger('+123') // true
isInteger('12.3') // false
isInteger('abc') // falseisFloat
判断是否为浮点数字符串。
typescript
function isFloat(value: string): boolean参数
| 参数 | 类型 | 描述 |
|---|---|---|
| value | string | 要检查的字符串 |
返回值
boolean - 是否为浮点数字符串
示例
typescript
isFloat('12.3') // true
isFloat('-12.3') // true
isFloat('+12.3') // true
isFloat('.3') // true
isFloat('123') // false
isFloat('abc') // falseisNumerical
判断是否为数值字符串(整数或浮点数)。
typescript
function isNumerical(value: string): boolean参数
| 参数 | 类型 | 描述 |
|---|---|---|
| value | string | 要检查的字符串 |
返回值
boolean - 是否为数值字符串
示例
typescript
isNumerical('123') // true
isNumerical('12.3') // true
isNumerical('-12.3') // true
isNumerical('abc') // falseisDigit
判断是否为纯数字字符串。
typescript
function isDigit(value: string): boolean参数
| 参数 | 类型 | 描述 |
|---|---|---|
| value | string | 要检查的字符串 |
返回值
boolean - 是否为纯数字字符串
示例
typescript
isDigit('123') // true
isDigit('0') // true
isDigit('12.3') // false
isDigit('-123') // false
isDigit('abc') // false