开发者

find the text and the strip the text at a certain point

开发者 https://www.devze.com 2023-01-17 20:46 出处:网络
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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号