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();
}
var text = "some text - is found";
var accom = text.split("-")[0];
alert(accom); // some text
[Demo]
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
精彩评论