0

I have a string like this,

var str = " This is a ?sample? text to ?extract? question marks separated texts into an array in ?Javascript?. "

I want a javascript array as below,

arr = {
        'sample',
        'extract',
        'Javascript'
      }
Ajith
  • 2,476
  • 2
  • 17
  • 38
  • This is fairly basic and therefore might not get the best reception. Assuming you don't know where to begin, I suggest you take a quick regex tutorial, which should make solving this task trivial – Aaron Feb 19 '20 at 10:08
  • Does this answer your question? [Get Substring between two characters using javascript](https://stackoverflow.com/questions/14867835/get-substring-between-two-characters-using-javascript) – Gary Thomas Feb 19 '20 at 10:09
  • "Please make a good faith attempt to solve the problem yourself first. If we can't see enough work on your part your question will likely be booed off the stage; it will be voted down and closed." see [How do I ask and answer homework questions?](https://meta.stackoverflow.com/a/334823/199263) – AndreasPizsa Feb 19 '20 at 10:10
  • Should `"foo??bar"` output `[]` or `['']` ? – Cid Feb 19 '20 at 10:17
  • What should be the output of `"?hello?world?"` ? – Cid Feb 19 '20 at 10:34

2 Answers2

1

You can alternate between keeping and ignoring letters each time you get to a "?". This will not include the text after an opening ? if there is no closing ?.

let str = "This ?? should ?only? leave ?four words? ?in? the ?result?. Ignoring if there is no ? closing question mark"
let result = []
let openingQuestionMark = false

let substring = ""

for (let key of str) {
  if (key === "?") {
    if (openingQuestionMark) {
      substring && result.push(substring)
      substring = ""
    }
    openingQuestionMark = !openingQuestionMark
    continue
  }

  if (openingQuestionMark) {
    substring += key
  }

}


console.log(result)
Jacob
  • 887
  • 1
  • 8
  • 17
1

You can split() and then filter() the results having an odd index :

// After the split('?'), an array will be created. One needs the odd indexes
//                0        1         2      3                         4                                 5      6  7
const str = " This is a ?sample? text to ?extract? question marks separated texts into an array in ?Javascript?. ?";

const arr = str.split('?').filter((word, index) => index % 2 !== 0 && word !== '');

console.log(arr);

This will be working only if you are sure that the input contains only an even number of ?. An input like "foo?bar" will output [ 'bar' ].

To make sure this work with any input, you can use RegEx :

function getWordsBetweenParenthesis(input)
{
  const pattern = /(?:\?)(.*?)(?:\?)/gm;
  const matches = input.matchAll(pattern);
  return [...matches].map(arr => arr[1]);
}

console.log(getWordsBetweenParenthesis(" This is a ?sample? text to ?extract? question marks separated texts into an array in ?Javascript?. "));
console.log(getWordsBetweenParenthesis("foo?bar"));
console.log(getWordsBetweenParenthesis("foo??bar"));
console.log(getWordsBetweenParenthesis("?hello?World?"));
console.log(getWordsBetweenParenthesis("?hello??World?"));
Cid
  • 14,968
  • 4
  • 30
  • 45