Question about string segmentation using a javascript-Regular Expression

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.