Bruno February 2016

jQuery splitting string with a) b) c) etc

I am trying to split this string:

A2188/15 NOTAMN Q) LQSB/QFAAH/IV/NBO/A /000/999/4349N01820E005 A) LQSA B) 1510250000 C) 1603262359 E) AD AND ATS HOURS OF SERVICE: MON-SUN 0500-2100

into something more readable and formatted like this:

A2188/15 NOTAMN 
Q) LQSB/QFAAH/IV/NBO/A /000/999/4349N01820E005
A) LQSA
B) 1510250000 
C) 1603262359 
E) AD AND ATS HOURS OF SERVICE: MON-SUN 0500-2100

I am getting this string as an xml response from an API. So I won't know how many alphabets there will be A) b) c) etc.

I can only split it using the bracket.

var string = sRawtext;
var myStringArray = sRawtext.split(')');

Answers


user2866534 February 2016

try to use regExp (pure js):

var a = "A2188/15 NOTAMN Q) LQSB/QFAAH/IV/NBO/A /000/999/4349N01820E005 A) LQSA B) 1510250000 C) 1603262359 E) AD AND ATS HOURS OF SERVICE: MON-SUN 0500-2100"
var b = new RegExp("/[A-z]{1}\)/g");
a.split(b); //["A2188/15 NOTAMN ", " LQSB/QFAAH/IV/NBO/A /000/999/4349N01820E005 ", " LQSA ", " 1510250000 ", " 1603262359 ", " AD AND ATS HOURS OF SERVICE: MON-SUN 0500-2100"]


Magicprog.fr February 2016

Use positive lookahead ((?=)) so you can keep the list:

var str = "A0505/16 NOTAMR A0490/16 Q) FAJA/QOBCE/IV/M/AE/000/045/2913S02631E005 A) FABL B) 1602051025 C) 1603311500 EST E) BRAM FISCHER INTL AP (2913S 02631E): (66FT HGT) ON THE APCH PATH OF RWY 30 F) GND G) 4495FT AMSL";
var myStringArray = str.split(/(?=\s[A-Za-z]\)\s)/);

/*Demo: */
document.getElementById("result").innerHTML += myStringArray.join("<br>");
<div id="result"></div>


gurvinder372 February 2016

As I understood your question, you need to first separate the header uptil A)

Check this fiddle demo

var str = "A2188/15 NOTAMN Q) LQSB/QFAAH/IV/NBO/A /000/999/4349N01820E005 A) LQSA B) 1510250000 C) 1603262359 E) AD AND ATS HOURS OF SERVICE: MON-SUN 0500-2100";

var heading = str.substring( 0, str.indexOf( "A)" ) ) ; //this is the heading

str = str.substring(heading.length);

var items = str.split( /(?=[A-Z]\)\s)/ ); //these are the items of your array

Post Status

Asked in February 2016
Viewed 1,050 times
Voted 7
Answered 3 times

Search




Leave an answer