找到文本并在某个点剥离文本

发布于 2024-09-25 10:01:25 字数 452 浏览 0 评论 0原文

我正在尝试编写此函数来获取文本值,然后从 - 符号和 - 符号本身之后剥离文本。例如

找到一些文本

会变成

一些文字

这是 iv 目前得到的内容,它只是删除了 -

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

    alert(accom);

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

}

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

通知家属抬走 2024-10-02 10:01:25
var text = "some text - is found";
var accom = text.split("-")[0];
alert(accom); // some text 

[演示]

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

[Demo]

原谅我要高飞 2024-10-02 10:01:25

您可以稍微更改一下正则表达式,以便它也匹配 - 之后的所有内容,如下所示:

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

你可以在这里尝试一下,如果你想让-之前的空格消失,也可以添加:/ -.*/

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

も星光 2024-10-02 10:01:25

Replace 会将字符串中出现的所有 - 替换为 ' ' 您需要结合 indexOf 查找 substr 函数

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文