How do I write a regular expression in javascript such that it parses the string "1.aaa 2.bbb 3.ccc"
into ["aaa","bbb","ccc"]
.
here is my code :
var str = "1.aaa 2.bbb 3.ccc";
var reg1 = /\d+\.(.*)\d+\./g; //sub regexp $1 match "aaa 2.bbb"
var reg2 = /\d+\.(.*?)\d+\./g; //sub regexp $1 match "aaa"
var reg3 = /\d+\.(.*?)(?=\d+\.)/g; //sub regexp $1 match "aaa" 、 "bbb"
var reg4 = /\d+\.(.*?)(?=\d+\.)|\d+\.(.*)/g; //sub regexp $1 match "aaa" 、 "bbb"、undefined,my expect is $1 match "aaa"、“bbb”、“ccc”
str.replace(reg1, function(match, p1, p2, offset, string) {
console.log("---reg1---")
console.log(p1)
})
str.replace(reg2, function(match, p1, p2, offset, string) {
console.log("---reg2---")
console.log(p1)
})
str.replace(reg3, function(match, p1, p2, offset, string) {
console.log("---reg3---")
console.log(p1)
})
str.replace(reg4, function(match, p1, p2, offset, string) {
console.log("---reg4---")
console.log(p1) //sub regexp $1 match "aaa" 、 "bbb"、undefined,my expect is $1 match "aaa"、“bbb”、“ccc”
})
Is there a simple solution to do this?