Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

javascript - Replace a substring of a given length after a spefic piece of text

I'm stuck :) Using javascript. I get an output from an instrument of which below is a partial outtake. I need to verify this data. On the line beginning 'MSH', after instrument field is a date and time
20210204220807+00:00
This repeats several time in the output and I want to strip it out of the string.

I will have the system date and time to the minute. For example 202102042208 . How can I use this to remove the whole text everywhere it appears? So replace 202102042208 + the next 8 characters.

No idea how I can do this? Any help is appreciated

MSH|^~&|New1|| Instrument 1||**20210204220807+00:00**||RSP^K11^RSP_K11|547CwlxiBkSMM9D2tSU+pw|P|2.5.1||||||UNICODE UTF-8|||LAB-27R^wegewg
MSA|AA|1234
QAK|query1234|OK|INIBAR^^99ROC
QPD|INIBAR^^99ROC|query1234|Sample1|50001|1|||||SERPLAS^^99ROC|^^99ROC|R 
question from:https://stackoverflow.com/questions/66054819/replace-a-substring-of-a-given-length-after-a-spefic-piece-of-text

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can use ^(MSH.*)202102042208.{8}

  • ^(MSH.*), capture match string start from MSH to **, it will be used for replacement or $1
  • .{8} match any characters with length of 8

var str = `MSH|^~&|New1|| Instrument 1||**20210204220807+00:00**||RSP^K11^RSP_K11|547CwlxiBkSMM9D2tSU+pw|P|2.5.1||||||UNICODE UTF-8|||LAB-27R^wegewg
MSA|AA|1234
QAK|query1234|OK|INIBAR^^99ROC
QPD|INIBAR^^99ROC|query1234|Sample1|50001|1|||||SERPLAS^^99ROC|^^99ROC|R 
MSH|^~&|New1|| Instrument 1||**20210204220807+00:00**||RSP^K11^RSP_K11|547CwlxiBkSMM9D2tSU+pw|P|2.5.1||||||UNICODE UTF-8|||LAB-27R^wegewg`

str = str.replace(/^(MSH.*)202102042208.{8}/gm, '$1Date_Removed')
console.log(str)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...