JavaScript Get letters in between other letters | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 1

JavaScript Get letters in between other letters

Hello.. I kinda need help using REGEX I never understood it , I tried multiple times.. But everytime I run into something requiring REGEX my mind just goes blank.. I have some integers in an array and some text behind them.. for example M:44, 45C:65, 66 I want to get everything after M: that's before C: then get everything after C: the numbers are actually char codes so I made them integers

26th Oct 2017, 6:56 PM
Sploit
Sploit - avatar
7 Antworten
+ 4
If I right understand, you have an array like: var data = [ "M:44", "45C:65", "66" ]; ... where all items are string? Or are your data of another type? Number, Object? or even a mix? Give us more description about your data representation if you want to be accuratly helped... even share your actual code using it ;)
26th Oct 2017, 7:32 PM
visph
visph - avatar
+ 3
/* Ok, you have a string such as "M:44, 45C:65, 66", and you want to extract "44, 45"? As you have always the pattern "M:"+value2extract+"C:"+secondValue and never have more than once time 'M' and only once "C", you can avoid use of regular expression: */ var data = "M:44, 45C:65, 66"; var i = data.indexOf('C'); var extract = data.substring(2,i); // data to be extracted start always at 3rd character (index: 2) and end at index of 'C' console.log(extract); // "44, 45" // Else, using regex: var re = /^M:([^C]*)C:/; // first '^' mark start of string, // () surround the part to be captured, // [^C] define a class of all characters excluding 'C' // * search for 0 or any previous element (any times any character appart 'C') extract = re.exec(data)[1]; // returned object by RegEcp.exec() method is an array-like // index 0 contains string matching rexgex // next indexes contains value of captured parenthesis groups (here only once, at index 1) console.log(extract); // "44, 45"
26th Oct 2017, 8:08 PM
visph
visph - avatar
+ 3
If you're interested by encoding/decoding variables in text format, you should look at JSON object: it allow you to stringify() and parse() saving time of handling it by yourself (and more efficiently ^^)... What I would do would be: > store my data in objects instead in a string directly: var data = {M:[], C:[]}; // create an empty model data.M.push(44); // populate inner array data.M.push(45); data['C'].push(65); data['C'].push(66); > when need to export to string: var datastr = JSON.stringify(data); // '{"M":[44,45],"C":[65,66]}' > when need to import string: var decoded = JSON.parse(datastr); > now you can use the decoded object as the initial 'data' object: var listM = decoded.M; // [44,45] > wich is already an array with integer values (if you have pushed integer initially, obviously ;P): var sum = listM[0]+listM[1]; // 89
26th Oct 2017, 9:10 PM
visph
visph - avatar
+ 1
I convert the message and amount of clicks to an array of char codes eventually I want to read it as data from a code in that format
26th Oct 2017, 7:50 PM
Sploit
Sploit - avatar
+ 1
I'm still so confused lmao but I will practice from the use of your first example once I get the hang of it , write a function for it and convert it to an integer to be placed into one of the variables , also what do you think about the idea of the game? I used something like it before, so thought I'd recreate it , you have to click the egg to unlock the message
26th Oct 2017, 8:55 PM
Sploit
Sploit - avatar
+ 1
That seems like a much better way to go about things lol , Thank you! I Been trying to increase my skills in JavaScript too
26th Oct 2017, 11:48 PM
Sploit
Sploit - avatar