Home  >  Article  >  Web Front-end  >  How to get the last few characters of a string in jquery

How to get the last few characters of a string in jquery

藏色散人
藏色散人Original
2023-03-21 09:27:552243browse

How to get the last few strings in jquery: 1. Create a front-end sample file; 2. Define a variable through var; 3. Through "var brand4=brand.substr(brand.length-4); $("#bank_card").val(brand4);" method can be used to obtain the last 4 digits of the string.

How to get the last few characters of a string in jquery

The operating environment of this tutorial: Windows 10 system, jquery3.6 version, Dell G3 computer.

How to get the last few characters of a string with jquery?

jq intercepts the last four digits of the string

var brand="236554123846512"
var brand4=brand.substr(brand.length-4);
$("#bank_card").val(brand4);

Related extensions:

jQuery substr() method

substr() method can be used in characters Extracts the specified number of characters starting from the start subscript from the string.

Syntax

stringObject.substr(start,length)

Parameters

start Required. The starting index of the substring to be extracted. Must be a numeric value. If negative, this parameter declares the position from the end of the string. That is, -1 refers to the last character in the string, -2 refers to the second to last character, and so on.

length Optional. The number of characters in the substring. Must be a numeric value. If this parameter is omitted, the string from the beginning to the end of stringObject is returned.

Return value

A new string containing length characters starting from the start of stringObject (including the character pointed to by start). If length is not specified, the returned string contains characters from start to the end of the stringObject.

Example 1

在本例中,我们将使用 substr() 从字符串中提取一些字符:
<script type="text/javascript">
var str="Hello world!"
document.write(
str.substr(3)
)
</script>

Output:

lo world!

Example 2

In this example, we will use substr() to extract some characters from the string :

<script type="text/javascript">
var str="Hello world!"
document.write(
str.substr(3,7)
)
</script>

Output:

lo worl

Recommended learning: jQuery video tutorial

The above is the detailed content of How to get the last few characters of a string in jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn