How to get the string just before specific character in JavaScript?

javascript substring after character
get string before character java
get first letter of string javascript
javascript remove part of string
javascript string split
javascript right string
javascript truncate string
get specific value from string javascript

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.

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', '')

How do I have JavaScript get a substring before a character?, split() returns an array of strings, split by the character you pass to it (in your case, the plus). Just use the first element of the array; it will have  Because strings must be written within quotes, JavaScript will misunderstand this string: var x = "We are the so-called "Vikings" from the north."; The string will be chopped to "We are the so-called ". The solution to avoid this problem, is to use the backslash escape character.

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 = => value.split(splitStr)[0]);
  return processedStr.join(" OR ");

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

javascript get string before a character, This is the what you should use to split: string.slice(0, string.indexOf("'"));. And then to handle your non existant value edge case: function  Pads the current string from the start with a given string and returns a new string of the length targetLength. String.prototype.repeat( count ) Returns a string consisting of the elements of the object repeated count times.

You can use a regex like this

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

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"

JavaScript String substr() Method, The substr() method extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters. Tip: To extract characters from the end of the string, use a negative start number Note: The substr() method does not change the original string. Extract only the first character:. I'm trying to extract the number before character "M" in a series of strings. The strings may look like: "107S33M15H" "33M100S" "12M100H33M" so basically there would be a sets of numbers separated by different characters, and "M" may show up more than once. For the example here, I would like my code to return:

JavaScript String substring() Method, The substring() method extracts the characters from a string, between two specified indices, and returns the new sub string. This method extracts the characters in a string Example. Extract only the first character: var str = "Hello world!"; The easiest way is probably just to split on your target word. my_string="hello python world , i'm a beginner " print my_string.split("world",1) [1] split takes the word (or character) to split on and optionally a limit to the number of splits. In this example split on "world" and limit it to only one split. improve this answer.

JavaScript: Get a part of string after a specified character, JavaScript: Get a part of string after a specified character. Last update on February 26 indexOf(char) + 1); else if(pos=='a') return str.substring(0, str.​indexOf(char)); else JavaScript: Tips of the Day. Returns true if the given  In this case, to remove all before the > character, just search for "*> " (without the quotes) and replace with nothing. Then all characters before "> " are removed. The opposite is also easy. To remove everything after the > character, just search for ">*" (without the quotes) and replace with nothing. Printer friendly version. You may also like:

String.prototype.substring(), The index of the first character to exclude from the returned substring. Return value. A new string containing the specified part of the given string. Get Substring - everything before certain char. Asked 10 years, 2 months ago. Active yesterday. Viewed 258k times. I'm trying to figure out the best way to get everything before the - character in a string. Some example strings are below. The length of the string before - varies and can be any length. 223232-1.jpg 443-2.jpg 34443553-5.jpg.

  • input.split("L")[0]...?
  • Might not work in case of April18L7. Just in case there's a longer version of Month names somewhere.
  • Apart from L7 is there some other sample input you can provide so that we can generalise solution.
  • @ShubhamGupta thanks for suggestion we have some more like this Apr12R0 or Jun12R0
  • @SiddAjmera thanks for your input, for april we denote like first three letters like this. Apr..
  • You are welcome, can you choose one of all answer like response to your issue