To split the string "1, 2, & #039; 3, 4 & #039;, 5", the result is [& #039; 1 & #039 ;, & #039; 2 & #039;, '','', '3, 4', & #039; 5 & #039;] ask me how to write a regular expression that I implemented using regular expressions. php code: {code ...} result :... to split the string ", '3, 4', 5", the result is ['1', '2', '','', '3, 4', '5']
Ask how to write a regular expression
========================================================== ================================
Php code is implemented by myself using regular expressions:
var_dump(preg_split("/[,]?'([^']+)'[,]?|[,]+?/", "1,2,6,7,,'3,4',5,8,9", 0, PREG_SPLIT_DELIM_CAPTURE));
Result:
array(9) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "6" [3]=> string(1) "7" [4]=> string(0) "" [5]=> string(3) "3,4" [6]=> string(1) "5" [7]=> string(1) "8" [8]=> string(1) "9"}
I wonder if there is any problem.
Reply content:
To split the string ", '3, 4', 5", the result is ['1', '2', '','', '3, 4', '5']
Ask how to write a regular expression
========================================================== ================================
Php code is implemented by myself using regular expressions:
var_dump(preg_split("/[,]?'([^']+)'[,]?|[,]+?/", "1,2,6,7,,'3,4',5,8,9", 0, PREG_SPLIT_DELIM_CAPTURE));
Result:
array(9) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "6" [3]=> string(1) "7" [4]=> string(0) "" [5]=> string(3) "3,4" [6]=> string(1) "5" [7]=> string(1) "8" [8]=> string(1) "9"}
I wonder if there is any problem.
The regular expression is not used, but this problem can be used.split()
var str = "1,2,,,'3,4',5";str.split(",");
= 0; $i--) { $result[$i] = trim($result[$i], "'"); } var_dump($result);
Result:
array(10) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" [4]=> string(1) "5" [5]=> string(1) "6" [6]=> string(0) "" [7]=> string(0) "" [8]=> string(0) "3,4" [9]=> string(4) "2333" }
It can be simplified and modified.
(Not the simplest answer.
Provide an idea:
Var str = ", '3, 4', 5"; // use the regular expression to replace ',' in '3, 4' with other characters, for example, '*', then 'split ', then check' * 'in each element, and change it back to var temp = str. replace (/'(\ w) +, (\ w) +'/, '$1*$2 '). split (','); str = temp. map (function (n) {return n. replace (/\*/,',');});
Hope to help ~ :)
('.*')|[^,]
In this case, try it!
Image Description