字符串解析器 [英] Parser for string
本文介绍了字符串解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个字符串,
str = "Date()+Abs(9)Day()+45"
我想要一个包含以下内容的数组:
I want an array that contains:
arr[0]=Date()
arr[1]=Abs(9)
arr[2]=Day()
我当前的解析器:
var temp = str.match(/\w+(\w+)/)
请帮助
推荐答案
这是一个非常简单的表达式.
This is a pretty easy expression.
/ -- START expression
\w+ -- One or more alpha-numeric (plus '_')
\( -- Literal opening parenthesis
[\w/-]* -- Zero or more alpha-numeric (plus '_', '/', and '-')
\) -- Literal closing parenthesis
/g -- END expression; Global match
var str = "Date(20/04/98)+Abs(-9)Day(20/04/98)+45"
var arr = str.match(/\w+\([\w/-]*\)/g);
console.log(JSON.stringify(arr, null, 2));
// arr[0] = Date(20/04/98)
// arr[1] = Abs(-9)
// arr[2] = Day(20/04/98)
- 信息
- Information
- Wikipedia
- Mozilla Developer Network
- Regular-Expressions.info
- RegexBuddy
- RegExr (推荐)
- Dan的工具-Regex Pal (与正则表达式测试器)
- 正则表达式101
- RegExr (Recommended)
- Dan's Tools - Regex Pal (same as Regex Tester)
- regular expressions 101
这篇关于字符串解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Information
查看全文