我有一个来自数据库的长字符串,我必须将其分解并能够将其放入json格式。我知道那部分我不会有任何问题,但我一直在努力将其分解。
LAST ADMISSION DATE,LAST ASSESSMENT DATE,LAST DISCHARGE DATE,LAST MH
SAFETY PLAN,NEXT MH APPT DATE,ON TRACK,PATIENT NAME,PATIENT PRF,PRF
REVIEW,^widgetcolumns\nHIGH RISK^widget,17|MENTAL HEALTH
CENTER,^widgetloc,42^instrID,PHQ9^instrName,S^instrScore|\nSUD^widget
inactive,3180601\nTRIAGE^widgetTRIAGE
WIDGET^widgettitle5^widgettype\nTRIAGE^widget,LAST APPOINTMENT,PATIENT
NAME,^widgetcolumns\nTRIAGE^widget,17|MENTAL HEALTH CENTER,26|MENTAL
HYGIENE-OPC,^widgetloc,\nHIGH RISK^widget,APPOINTMENT DATE,LAST
ASSESSMENT DATE,MH LAST APPT
DATE,^criteriafields,RG,GE,GE,^criteriaexp,01/01/2016-
12/31/2018,01/01/2016,01/01/2010,^criteriaval\nTRIAGE^widget,APPOINTMENT
DATE,^criteriafields,GE,^criteriaexp,01/01/2015,^criteriaval\nERROR:
TRIAGE^wdgt,,^BAD APPT DATES\nHIGH
RISK^widget,1^count,3^dfn,EIGHT,PATIENT^patname,
09/04/2018^reviewdate,ACTIVE^status,HIGH RISK FOR SUICIDE^flag\nHIGH
RISK^widget,2^count,379^dfn,SEVENTYNINE,PATIENT^patname,
07/01/2018^reviewdate,ACTIVE^status,HIGH RISK FOR SUICIDE^flag\nHIGH
RISK^widget,3^count,766^dfn,SEVENTYONE,PATIENT^patname,
06/12/2018^reviewdate,ACTIVE^status,HIGH RISK FOR SUICIDE^flag\nHIGH
RISK^widget,4^count,100848^dfn,AVIVAPATIENT,EIGHT^patname,
01/24/2018^reviewdate,ACTIVE^status,HIGH RISK FOR SUICIDE^flag\n
对于这个超长字符串,我事先表示歉意,但我非常需要
"HIGH RISK^widget,1^count,3^dfn,EIGHT,PATIENT^patname,"
从该字符串到底部。因此,所有超出引号的内容都将被丢弃。
有没有办法让我的头靠在墙上?没什么可以硬编码的,因为根据登录的人,所有数据都是不同的。
更新:对于那些询问所需输出的用户。我希望这是一串没有其他一切的大串。
"HIGH RISK^widget,1^count,3^dfn,EIGHT,PATIENT^patname,
09/04/2018^reviewdate,ACTIVE^status,HIGH RISK FOR SUICIDE^flag\n
HIGH RISK^widget,2^count,379^dfn,SEVENTYNINE,PATIENT^patname,
07/01/2018^reviewdate,ACTIVE^status,HIGH RISK FOR SUICIDE^flag\n
HIGH RISK^widget,3^count,766^dfn,SEVENTYONE,PATIENT^patname,
06/12/2018^reviewdate,ACTIVE^status,HIGH RISK FOR SUICIDE^flag\n
HIGH RISK^widget,4^count,100848^dfn,AVIVAPATIENT,EIGHT^patname,
01/24/2018^reviewdate,ACTIVE^status,HIGH RISK FOR SUICIDE^flag\n"
提前感谢您的帮助!
答案 0 :(得分:1)
您可以使用此正则表达式:
[\s\S]*?(?=HIGH\s*?RISK\^widget,1\^)
示例:https://regex101.com/r/HNvvpc/1/
完整的JS:
var String = "LAST ADMISSION DATE,LAST ASSESSMENT DATE,LAST DISCHARGE DATE,LAST MH SAFETY PLAN,NEXT MH APPT DATE,ON TRACK,PATIENT NAME,PATIENT PRF,PRF REVIEW,^widgetcolumns\nHIGH RISK^widget,17|MENTAL HEALTH CENTER,^widgetloc,42^instrID,PHQ9^instrName,S^instrScore|\nSUD^widget inactive,3180601\nTRIAGE^widgetTRIAGE WIDGET^widgettitle5^widgettype\nTRIAGE^widget,LAST APPOINTMENT,PATIENT NAME,^widgetcolumns\nTRIAGE^widget,17|MENTAL HEALTH CENTER,26|MENTAL HYGIENE-OPC,^widgetloc,\nHIGH RISK^widget,APPOINTMENT DATE,LAST ASSESSMENT DATE,MH LAST APPT DATE,^criteriafields,RG,GE,GE,^criteriaexp,01/01/2016- 12/31/2018,01/01/2016,01/01/2010,^criteriaval\nTRIAGE^widget,APPOINTMENT DATE,^criteriafields,GE,^criteriaexp,01/01/2015,^criteriaval\nERROR: TRIAGE^wdgt,,^BAD APPT DATES\nHIGH RISK^widget,1^count,3^dfn,EIGHT,PATIENT^patname,09/04/2018^reviewdate,ACTIVE^status,HIGH RISK FOR SUICIDE^flag\nHIGH RISK^widget,2^count,379^dfn,SEVENTYNINE,PATIENT^patname,07/01/2018^reviewdate,ACTIVE^status,HIGH RISK FOR SUICIDE^flag\nHIGH RISK^widget,3^count,766^dfn,SEVENTYONE,PATIENT^patname,06/12/2018^reviewdate,ACTIVE^status,HIGH RISK FOR SUICIDE^flag\nHIGH RISK^widget,4^count,100848^dfn,AVIVAPATIENT,EIGHT^patname,01/24/2018^reviewdate,ACTIVE^status,HIGH RISK FOR SUICIDE^flag\n"
console.log(String.replace(/[\s\S]*?(?=HIGH\s*?RISK\^widget,1\^)/, ''));
要删除^
和数组内的所有内容,请将此简单的正则表达式\^.*
与循环一起使用。示例:
var Arr = ["HIGH RISK^widget", "1^count", "3^dfn", "EIGHT", "PATIENT^patname", "09/04/2018^reviewdate", "ACTIVE^status", "HIGH RISK FOR SUICIDE^flag"];
for (i = 0; i < Arr.length; i++) {
console.log(Arr[i].replace(/\^.*/, ''));
}
答案 1 :(得分:0)
您可以先去除所有换行符和换行符,然后用“ HIGH”替换从字符串开头到HIGH的第一个实例的所有内容
var str = `LAST ADMISSION DATE,LAST ASSESSMENT DATE,LAST DISCHARGE DATE,LAST MH
SAFETY PLAN,NEXT MH APPT DATE,ON TRACK,PATIENT NAME,PATIENT PRF,PRF
REVIEW,^widgetcolumns\nHIGH RISK^widget,17|MENTAL HEALTH
CENTER,^widgetloc,42^instrID,PHQ9^instrName,S^instrScore|\nSUD^widget
inactive,3180601\nTRIAGE^widgetTRIAGE
WIDGET^widgettitle5^widgettype\nTRIAGE^widget,LAST APPOINTMENT,PATIENT
NAME,^widgetcolumns\nTRIAGE^widget,17|MENTAL HEALTH CENTER,26|MENTAL
HYGIENE-OPC,^widgetloc,\nHIGH RISK^widget,APPOINTMENT DATE,LAST
ASSESSMENT DATE,MH LAST APPT
DATE,^criteriafields,RG,GE,GE,^criteriaexp,01/01/2016-
12/31/2018,01/01/2016,01/01/2010,^criteriaval\nTRIAGE^widget,APPOINTMENT
DATE,^criteriafields,GE,^criteriaexp,01/01/2015,^criteriaval\nERROR:
TRIAGE^wdgt,,^BAD APPT DATES\nHIGH
RISK^widget,1^count,3^dfn,EIGHT,PATIENT^patname,
09/04/2018^reviewdate,ACTIVE^status,HIGH RISK FOR SUICIDE^flag\nHIGH
RISK^widget,2^count,379^dfn,SEVENTYNINE,PATIENT^patname,
07/01/2018^reviewdate,ACTIVE^status,HIGH RISK FOR SUICIDE^flag\nHIGH
RISK^widget,3^count,766^dfn,SEVENTYONE,PATIENT^patname,
06/12/2018^reviewdate,ACTIVE^status,HIGH RISK FOR SUICIDE^flag\nHIGH
RISK^widget,4^count,100848^dfn,AVIVAPATIENT,EIGHT^patname,
01/24/2018^reviewdate,ACTIVE^status,HIGH RISK FOR SUICIDE^flag\n`
function output(){
str = str.replace(/\n|\\n/g, ' ')
.replace(/^.+?high/i, 'HIGH')
return str
}
document.getElementById('output').textContent = output()
<div id="output"></div>
答案 2 :(得分:0)
您可以使用split
,slice
和join
:
var str = "LAST ADMISSION DATE,LAST ASSESSMENT DATE,LAST DISCHARGE DATE,LAST MH SAFETY PLAN,NEXT MH APPT DATE,ON TRACK,PATIENT NAME,PATIENT PRF,PRF REVIEW,^widgetcolumns\nHIGH RISK^widget,17|MENTAL HEALTH CENTER,^widgetloc,42^instrID,PHQ9^instrName,S^instrScore|\nSUD^widget inactive,3180601\nTRIAGE^widgetTRIAGE WIDGET^widgettitle5^widgettype\nTRIAGE^widget,LAST APPOINTMENT,PATIENT NAME,^widgetcolumns\nTRIAGE^widget,17|MENTAL HEALTH CENTER,26|MENTAL HYGIENE-OPC,^widgetloc,\nHIGH RISK^widget,APPOINTMENT DATE,LAST ASSESSMENT DATE,MH LAST APPT DATE,^criteriafields,RG,GE,GE,^criteriaexp,01/01/2016- 12/31/2018,01/01/2016,01/01/2010,^criteriaval\nTRIAGE^widget,APPOINTMENT DATE,^criteriafields,GE,^criteriaexp,01/01/2015,^criteriaval\nERROR: TRIAGE^wdgt,,^BAD APPT DATES\nHIGH RISK^widget,1^count,3^dfn,EIGHT,PATIENT^patname,09/04/2018^reviewdate,ACTIVE^status,HIGH RISK FOR SUICIDE^flag\nHIGH RISK^widget,2^count,379^dfn,SEVENTYNINE,PATIENT^patname,07/01/2018^reviewdate,ACTIVE^status,HIGH RISK FOR SUICIDE^flag\nHIGH RISK^widget,3^count,766^dfn,SEVENTYONE,PATIENT^patname,06/12/2018^reviewdate,ACTIVE^status,HIGH RISK FOR SUICIDE^flag\nHIGH RISK^widget,4^count,100848^dfn,AVIVAPATIENT,EIGHT^patname,01/24/2018^reviewdate,ACTIVE^status,HIGH RISK FOR SUICIDE^flag\n"
console.log(str.split('\n').slice(1).join('\\n'))
如果您需要通过换行符加入字幕,请使用.join('\n')
。