views:

21

answers:

3

I am trying to write this funtion to grab the text value and then strip the text from after the - symbol and the - symbol itself. eg

some text - is found

would become

some text

This is what iv got so far currently it just removes the -

$.keynav.enterDown = function () {
      var accom = $('#suggestions p.keynavon').text().replace(/-/g,'');

    alert(accom);

       $('#search input#q').val(accom);
      $("#form").submit();

}
A: 

replace will replace all occurence of - from your string with ' ' you need to look for substr function in combination with indexOf

sushil bharwani
+3  A: 
var text = "some text - is found";
var accom = text.split("-")[0];
alert(accom); // some text 

[Demo]

galambalazs
perfect thank you
AJFMEDIA
+1  A: 

You can change your regex a bit so it's matching everything after the - as well, like this:

var accom = $('#suggestions p.keynavon').text().replace(/-.*/,'');

You can give it a try here, if you want the space before the - gone, add that as well: / -.*/.

Nick Craver
cheers once again nick, your solution worked aswell :)
AJFMEDIA