26

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 am using the follwing code to acheive this.

$menuItem.text().match(/[^']*/)[0]

My problem is that if the string is something like €0.88 I wish to get an empty string returned. However I get back the full string of €0.88.

What I am I doing wrong with the match?

3
  • I don't quite understand the 'problem' you describe; if the string is equal to €0.88 you want to get an empty string? Or if the string is 14' - €0.88 you want an empty string? Commented Aug 25, 2012 at 18:45
  • I only want a string returned if the is a quote in it. The string I wish to have returned is the characters before the quote. Again if no quote then nothing returned. Commented Aug 25, 2012 at 18:49
  • If always an int or no int, then try var val = parseInt(str); if isNaN(val) val = "" Commented Aug 25, 2012 at 19:07

5 Answers 5

41

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

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

2 Comments

Why not save the value str.indexOf() in a variable so you don't have to search the string twice?
Why not pass the character in also and make this reusable?
6

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("");
}

1 Comment

@3on - I was just stating a reason for posting an answer after there were already a bunch of other answers. If you like a different answer better, feel free to vote for it. This answer was not offered at the time I posted it. I edited my answer to change the wording.
5

try this

str.substring(0,str.indexOf("'"));

4 Comments

Doesn't handle the case of quote not being in the string.
It does seem to work I guess - kind of an odd use of substring() passing -1 to it. I wouldn't personally feel comfortable with that illegal argument without testing in a bunch of browsers.
as a matter of fact I did test it in IE8, FF, chrome and Opera. Thats the simplest solution to the question.
You're welcome to write code that way. I prefer to stay within the legal bounds of the argument specs, even it's a few more lines of code.
0

Here is an underscore mixin in coffescript

_.mixin
  substrBefore : ->
    [char, str] = arguments
    return "" unless char?
    fn = (s)-> s.substr(0,s.indexOf(char)+1)
    return fn(str) if str?
    fn

or if you prefer raw javascript : http://jsfiddle.net/snrobot/XsuQd/

You can use this to build a partial like:

var beforeQuote = _.substrBefore("'");
var hasQuote = beforeQuote("14' - €0.88"); // hasQuote = "14'"
var noQoute  = beforeQuote("14 €0.88");    // noQuote =  ""

Or just call it directly with your string

var beforeQuote = _.substrBefore("'", "14' - €0.88"); // beforeQuote = "14'"

I purposely chose to leave the search character in the results to match its complement mixin substrAfter (here is a demo: http://jsfiddle.net/snrobot/SEAZr/ ). The later mixin was written as a utility to parse url queries. In some cases I am just using location.search which returns a string with the leading ?.

Comments

0

I use "split":

let string = "one-two-three";

let um = string.split('-')[0];
let dois = string.split('-')[1];
let tres = string.split('-')[2];

document.write(tres) //three

Comments

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.