javascript get string before a character

前端 未结 4 1277
心在旅途
心在旅途 2021-02-05 05:16

I have a string that and I am trying to extract the characters before the quote.

Example is extract the 14 from 14\' - €14.99

I

4条回答
  •  走了就别回头了
    2021-02-05 06:03

    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 split(str) {
     var i = str.indexOf("'");
    
     if(i > 0)
      return  str.slice(0, i);
     else
      return "";     
    }
    

    Demo on JsFiddle

提交回复
热议问题