Latest web development tutorials

JavaScript lastIndexOf () method

String Object Reference JavaScript String Object

Examples

Find the string "planet" last seen:

var str="Hello planet earth, you are a great planet.";
var n=str.lastIndexOf("planet");

n output:


try it"

Definition and Usage

lastIndexOf () method returns the position of a specified string value last appeared in a string at a specified location from the search forward.

Note: This method is retrieved from the head to the tail string string Object, to see if it contains a substring searchvalue.Start retrieving location (not specified fromindex time) at the end of the fromindex string or strings. If you find a searchvalue, the first character position in stringObject searchvalue in return. stringObject character position is zero-based.

If no matching string is returned -1.

Note: The lastIndexOf () method is case sensitive!

Tip: You can also refer a similar manner to the indexOf () .


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support lastIndexOf () method


grammar

string.lastIndexOf( searchvalue , start )

Parameter Value

参数 描述
searchvalue 必需。规定需检索的字符串值。
start 可选的整数参数。规定在字符串中开始检索的位置。它的合法取值是 0 到 stringObject.length - 1。如省略该参数,则将从字符串的最后一个字符处开始检索。

return value

类型 描述
Number 查找的字符串最后出现的位置,如果没有找到匹配字符串则返回 -1。

technical details

JavaScript version: 1.0


More examples

Examples

From the first 20 characters Find the string "planet" last seen,:

var str="Hello planet earth, you are a great planet.";
var n=str.lastIndexOf("planet",20);

n output:


try it"



String Object Reference JavaScript String Object