-1

I have a string that looks like this. Thi string is taken from a file where each of the keys are in different lines.

languagesKnown = 'Mother-Tongue : Spanish OtherLanguages: English Major: German' 

It can also look like

languagesKnown = 'Mother-Tongue:   OtherLanguages: English Major: German' 

I want to check which value is set to Mother-Tongue.

content:string[] = languagesKnown..split('\n')

gives me a string array, But I want to see if something is set to it or if it is empty like above.

learningUser
  • 428
  • 3
  • 8
  • 21
  • If the keys are in different lines then you can split the string by `\n` first to get individual lines. Now look for a line that starts with Mother-Tongue and split it by the colon, then trim() the result: https://jsfiddle.net/1o36efmy/ –  Jul 08 '22 at 08:28
  • regex is your way to go https://stackoverflow.com/questions/6109882/regex-match-all-characters-between-two-strings – Mohamed-Yousef Jul 08 '22 at 08:34
  • @ChrisG Your solution is perfect, If you give this as an answer and not in comment I can mark it as answer – learningUser Jul 08 '22 at 08:36
  • 1
    You're welcome but it's a dupe: [Get value from key-value string](https://stackoverflow.com/questions/70693743/get-value-from-key-value-string) (everything is, so I never post answers now) –  Jul 08 '22 at 08:40
  • This format is very close to be error-prone but it can be processed by a single regex as long as one can assure that the key always is a single word (no whitespace) or a single compound-word connected by dashes ... which matches following key pattern ... `[\w-]+` ... the entire regex then was ... [`/(?[\w-]+)\s*:\s*(?.*?)\s*(?=(?:[\w-]+\s*:)|$)/gm`](https://regex101.com/r/V9H5gq/1) – Peter Seliger Jul 08 '22 at 08:51
  • The duplicate link actually does not provide a solution to the OP's problem. – Peter Seliger Jul 08 '22 at 09:26

3 Answers3

0

try using split:

const examples = ['Mother-Tongue: Spanish OtherLanguages: English Major: German', 'Mother-Tongue:  OtherLanguages: English Major: German']
const values = examples.map(string => string.split(/[a-zA-Z-]*:/).slice(1))

for (value of values) {
  console.log(value[0].replace(/ /g, '') !== '')
}
wardialer
  • 490
  • 3
  • 7
-1

Try to split the string into an array of strings with : as the delimitier character.

The only problem could be the nonuniform formatting. So a solution could be counting the whitespaces. If there is more than 1 whitespace after a colon -> empty value.

qds
  • 1
  • 2
-1

With a regex you can easily find if there is a Mother Tongue and get its value.

let languagesKnown1 = 'Mother-Tongue : Spanish OtherLanguages: English Major: German';
let languagesKnown2 = 'Mother-Tongue:   OtherLanguages: English Major: German';

function getMotherTongue(languagesKnown){
    let regexMatch = languagesKnown.match(/Mother-Tongue : \w+/)
    if(regexMatch != null && regexMatch.length > 0)
      return regexMatch[0].split(":")[1].trim()

    return "No Mother Tongue!"
}

console.log(getMotherTongue(languagesKnown1))
console.log(getMotherTongue(languagesKnown2))