Question

I am trying to write this function 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();

}
Was it helpful?

Solution

var text = "some text - is found";
var accom = text.split("-")[0];
alert(accom); // some text 

[Demo]

OTHER TIPS

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: / -.*/.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top