Javascript javascript获取字符前的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12124623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:09:35  来源:igfitidea点击:

javascript get string before a character

javascriptjquery

提问by Keith Power

I have a string that and I am trying to extract the characters before the quote.

我有一个字符串,我试图在引号之前提取字符。

Example is extract the 14from 14' - 14.99

示例是从14' - 14.99 中提取14

I am using the follwing code to acheive this.

我正在使用以下代码来实现这一点。

$menuItem.text().match(/[^']*/)[0]

My problem is that if the string is something like 0.88 I wish to get an empty string returned. However I get back the full string of 0.88.

我的问题是,如果字符串类似于 0.88,我希望返回一个空字符串。但是我得到了 0.88 的完整字符串。

What I am I doing wrong with the match?

我在比赛中做错了什么?

回答by 3on

This is the what you should use to split:

这是您应该用来拆分的内容:

string.slice(0, string.indexOf("'"));

And then to handle your non existant value edge case:

然后处理您不存在的价值边缘情况:

function split(str) {
 var i = str.indexOf("'");

 if(i > 0)
  return  str.slice(0, i);
 else
  return "";     
}

Demo on JsFiddle

JsFiddle 上的演示

回答by jfriend00

Nobody seems to have presented what seems to me as the safest and most obvious option that covers each of the cases the OP asked about so I thought I'd offer this:

似乎没有人提出在我看来是最安全和最明显的选项,涵盖了 OP 询问的每个案例,所以我想我会提供这个:

function getCharsBefore(str, chr) {
    var index = str.indexOf(chr);
    if (index != -1) {
        return(str.substring(0, index));
    }
    return("");
}

回答by nandu

try this

尝试这个

str.substring(0,str.indexOf("'"));

回答by jhnstn

Here is an underscore mixin in coffescript

这是 coffescript 中的下划线 mixin

_.mixin
  substrBefore : ->
    [char, str] = arguments
    return "" unless char?
    fn = (s)-> s.substr(0,s.indexOf(char)+1)
    return fn(str) if str?
    fn

or if you prefer raw javascript : http://jsfiddle.net/snrobot/XsuQd/

或者,如果您更喜欢原始 javascript:http: //jsfiddle.net/snrobot/XsuQd/

You can use this to build a partial like:

您可以使用它来构建部分,如:

var beforeQuote = _.substrBefore("'");
var hasQuote = beforeQuote("14' - 0.88"); // hasQuote = "14'"
var noQoute  = beforeQuote("14 0.88");    // noQuote =  ""

Or just call it directly with your string

或者直接用你的字符串调用它

var beforeQuote = _.substrBefore("'", "14' - 0.88"); // beforeQuote = "14'"

I purposely chose to leave the search character in the results to match its complement mixin substrAfter(here is a demo: http://jsfiddle.net/snrobot/SEAZr/). The later mixin was written as a utility to parse url queries. In some cases I am just using location.searchwhich returns a string with the leading ?.

我故意选择在结果中保留搜索字符以匹配其补码 mixin substrAfter(这是一个演示:http: //jsfiddle.net/snrobot/SEAZr/)。后来的 mixin 是作为解析 url 查询的实用程序编写的。在某些情况下,我只是使用location.searchwhich 返回一个带前导的字符串?