简体   繁体   English

Javascript用模式分割字符串

[英]Javascript split the string with patterns

String: 串:

Q$: any character, number goes here.
A$: Answer goes here
C$: c,c1,c2,c3

I want the final output as follows 我希望最终输出如下

  [Q$:=any character, number goes here.,
   A$:=Answer goes here,
   C$:=c,c1,c2,c3
  ]

If there are more than one string, then array should have all the values. 如果有多个字符串,则数组应具有所有值。

Eg: 例如:

String:

Q$: any character, number goes here.
A$: Answer goes here
C$: c,c1,c2,c3

Q$: any character, number goes here.A$: Answer goes here
C$: c,c1,c2,c3

Q$: any character, number goes here.    A$: Answer goes here

Q$: any character, number goes here.
A$: Answer goes here
C$: c,c1,c2

Final array should have all the items as an array to validate. 最终数组应将所有项目作为要验证的数组。

I have tried 我努力了

 $(function() { var y = Array(); str = `Q$:ADD A GOOD QUESTION A$:ANSWER C$:choice0, choice1, choice2, choice2`; str.replace(/([ACQ]\\$:[\\S]*)(.*)/gmi, function(match, p1, p2) { y.push([p1, p2]); }); console.log(y); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

I am missing something. 我想念一些东西。 It works in few cases. 它在少数情况下有效。 In few it doesn't. 在少数情况下不是。 Any suggestions to split them out. 任何建议将它们分开。

What I have understood, as per your requirement, and assuming your output as a valid JSON object, Assuming order of Q$,A$,C$ will be always same, try this code 据我了解,根据您的要求,并假设您的输出为有效的JSON对象,假设Q $,A $,C $的顺序始终相同,请尝试以下代码

var str = "Your String"
//Add LineBreaks for making a valid format to make our search easier
var formattedOutput = str.replace(/[ACQ]\$/gm, function($0) {
    return "\n" + $0
})

//Split with all LineBreaks
var arr = formattedOutput.trim().split(/\n+/)

var res = [];
for(var i=0;i<arr.length;i++) {
    if(arr[i].indexOf("Q$") != -1) {
        res.push({
            "Q$":arr[i].trim().split(/Q\$:\s+/)[1]
        })
    } else {
        if(arr[i].indexOf("A$") != -1) {
console.log(arr[i].trim().split(/A\$:\s+/))
            res[res.length-1]["A$"] = arr[i].trim().split(/A\$:\s+/)[1]
        } else if(arr[i].indexOf("C$") != -1) {
            res[res.length-1]["C$"] = arr[i].trim().split(/C\$:\s+/)[1]
        }
    }
}
console.log(res)

 var str = $("#test").val() //Add LineBreaks for making a valid format to make our search easier var formattedOutput = str.replace(/[ACQ]\\$/gm, function($0) { return "\\n" + $0 }) //Split with all LineBreaks var arr = formattedOutput.trim().split(/\\n+/) var res = []; for(var i=0;i<arr.length;i++) { if(arr[i].indexOf("Q$") != -1) { res.push({ "Q$":arr[i].trim().split(/Q\\$:\\s+/)[1] }) } else { if(arr[i].indexOf("A$") != -1) { res[res.length-1]["A$"] = arr[i].trim().split(/A\\$:\\s+/)[1] } else if(arr[i].indexOf("C$") != -1) { res[res.length-1]["C$"] = arr[i].trim().split(/C\\$:\\s+/)[1] } } } $("#output").html(JSON.stringify(res)) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea id="test" rows="10" tabindex="3"> Q$: any character, number goes here. A$: Answer goes here C$: c,c1,c2,c3 Q$: any character, number goes here.A$: Answer goes here C$: c,c1,c2,c3 Q$: any character, number goes here. A$: Answer goes here Q$: any character, number goes here. A$: Answer goes here C$: c,c1,c2 </textarea> <div> <span id="output"></span> </div> 

Working Fiddle 工作小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM