Javascript 如何在jquery中子串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4126762/
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 11:01:35  来源:igfitidea点击:

How to substring in jquery

javascriptjquerystring

提问by Gorge

How can I use jquery on the client side to substring "nameGorge" and remove "name" so it outputs just "Gorge"?

如何在客户端使用 jquery 对“nameGorge”进行子字符串化并删除“name”,使其仅输出“Gorge”?

var name = "nameGorge"; //output Gorge

回答by Justin Niessner

No jQuery needed! Just use the substring method:

不需要jQuery!只需使用子字符串方法:

var gorge = name.substring(4);

Or if the text you want to remove isn't static:

或者,如果您要删除的文本不是静态的:

var name = 'nameGorge';
var toRemove = 'name';
var gorge = name.replace(toRemove,'');

回答by user113716

Using .split(). (Second version uses .slice()and .join()on the Array.)

使用.split(). (第二个版本在 Array 上使用.slice().join()。)

var result = name.split('name')[1];
var result = name.split('name').slice( 1 ).join(''); // May be a little safer

Using .replace().

使用.replace().

var result = name.replace('name','');

Using .slice()on a String.

使用.slice()上的绳子。

var result = name.slice( 4 );

回答by drew

Standard javascript will do that using the following syntax:

标准 javascript 将使用以下语法做到这一点:

string.substring(from, to)

string.substring(从,到)

var name = "nameGorge";
var output = name.substring(4);

Read more here: http://www.w3schools.com/jsref/jsref_substring.asp

在此处阅读更多信息:http: //www.w3schools.com/jsref/jsref_substring.asp

回答by casablanca

That's just plain JavaScript: see substringand substr.

这只是普通的 JavaScript:参见substringsubstr

回答by Maurizio Cucchiara

You don't need jquery in order to do that.

你不需要 jquery 来做到这一点。

var placeHolder="name";
var res=name.substr(name.indexOf(placeHolder) + placeHolder.length);

回答by Maurizio Cucchiara

var name = "nameGorge";
name.match(/[A-Z].*/)[0]

回答by aaronofleonard

Yes you can, although it relies on Javascript's inherent functionality and not the jQuery library.

是的,您可以,尽管它依赖于 Javascript 的固有功能而不是 jQuery 库。

http://www.w3schools.com/jsref/jsref_substr.aspThe substrfunction will allow you to extract certain parts of the string.

http://www.w3schools.com/jsref/jsref_substr.aspsubstr函数将允许您提取字符串的某些部分。

Now, if you're looking for a specific string or character to use to find what part of the string to extract, you can make use of the indexOf function as well. http://www.w3schools.com/jsref/jsref_IndexOf.asp

现在,如果您正在寻找特定的字符串或字符来查找要提取的字符串的哪一部分,您也可以使用 indexOf 函数。 http://www.w3schools.com/jsref/jsref_IndexOf.asp

The question is somewhat vague though; even just link textwith 'name' will achieve the desired result. What's the criteria for getting your substring, exactly?

不过这个问题有点含糊;即使只是用“名称”链接文本也能达到预期的效果。获取子字符串的标准是什么?

回答by Jean Paul A.K.A el_vete

How about the following?

以下情况如何?

<script charset='utf-8' type='text/javascript'>
  jQuery(function($) { var a=$; a.noConflict();
    //assumming that you are using an input text 
    //  element with the text "nameGorge"
    var itext_target = a("input[type='text']:contains('nameGorge')");
    //gives the second part of the split which is 'Gorge'
    itext_target.html().split("nameGorge")[1];
    ...
  });
</script>