0

I have couple of strings like this:

  • Mar18L7
  • Oct13H0L7

I need to grab the string like:

  • Mar18
  • Oct13H0

Could any one please help on this using JavaScript? How can I split the string at the particular character?

Many Thanks in advance.

9
  • 1
    input.split("L")[0]...? Commented Sep 21, 2018 at 16:00
  • Might not work in case of April18L7. Just in case there's a longer version of Month names somewhere. Commented Sep 21, 2018 at 16:03
  • Apart from L7 is there some other sample input you can provide so that we can generalise solution. Commented Sep 21, 2018 at 16:07
  • @ShubhamGupta thanks for suggestion we have some more like this Apr12R0 or Jun12R0 Commented Sep 21, 2018 at 16:10
  • @SiddAjmera thanks for your input, for april we denote like first three letters like this. Apr.. Commented Sep 21, 2018 at 16:11

3 Answers 3

3

For var str = 'Mar18L7';

Try any of these:

  1. str.substr(0, str.indexOf('L7'));

  2. str.split('L7')[0]

  3. str.slice(0, str.indexOf('L7'))

  4. str.replace('L7', '')

Sign up to request clarification or add additional context in comments.

Comments

1

Based on input that is given it I have created following function which can take n string in array and return the output in the format you have given. Check if this helps and if some use case is missed.

function generateStr(arr, splitStr) {
  const processedStr = arr.map(value => value.split(splitStr)[0]);
  return processedStr.join(" OR ");
}

console.log(generateStr(["Mar18L7", "Oct13H0L7"], "L7"));

Comments

1

You can use a regex like this

var data = ["Mar18L7", "Oct13H0L7"];
var regex = /^([a-zA-Z0-9]+)\L[a-zA-Z0-9]+$/;
var output = []
data.forEach(function(el){
    var matches = el.match(regex);
    output.push(matches[1]);
}); 

output variable will be equal to ['Mar18', 'Oct13H0'] and you can join all value usin the .join method on output array

var chain = output.join(" OR ");
// chain will be equal to "Mar18 OR Oct13H0"

1 Comment

You are welcome, can you choose one of all answer like response to your issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.