javascript get string before a character

前端 未结 4 1280
心在旅途
心在旅途 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 05:42

    Nobody seems to have presented what seems to me as the safest and most obvious option that covers each of the cases the OP asked about so I thought I'd offer this:

    function getCharsBefore(str, chr) {
        var index = str.indexOf(chr);
        if (index != -1) {
            return(str.substring(0, index));
        }
        return("");
    }
    

提交回复
热议问题