Home > Web Front-end > JS Tutorial > body text

What does the $ symbol mean in js

下次还敢
Release: 2024-05-06 14:48:17
Original
1055 people have browsed it

In JavaScript, the $ symbol is a wildcard character that represents any string. It is usually used to match any character or character sequence that often appears at the end of the regular expression, matching any character or string sequence at the end of the input string.

What does the $ symbol mean in js

The $ symbol in JavaScript

In JavaScript, the $ symbol is a wildcard character that represents any string. It is often used to match any character or sequence of characters, making regular expressions cleaner and more versatile.

Usage

$ The symbol is usually used at the end of a regular expression and will match any character or character sequence at the end of the input string. For example:

<code class="javascript">const regex = /s$/;
const string = "this";
const match = regex.test(string); // true

const regex2 = /(.)$/;
const string2 = "hello";
const match2 = regex2.test(string2); // true</code>
Copy after login

The first regular expression (/s$/) matches a string ending with the letter "s". The second regular expression (/(.)$/) matches a string ending with any character, captured by parentheses (.).

Examples

$ symbols can be used in various scenarios, for example:

  • Match end of line: is used to ensure that the regular expression only matches Enter the text at the end of the string.
  • Match word boundaries: Used in conjunction with \b (word boundary) to match the beginning or end of a word.
  • Lazy matching: means matching the minimum number of characters, used in combination with ? (lazy quantifier).

Note

It’s worth noting that the $ symbol has other meanings in JavaScript, depending on the context:

  • jQuery: In the jQuery library, the $ symbol is used to represent jQuery objects.
  • ES6: In ES6, the $ sign is the beginning or end marker of a template string.

Therefore, when using the $ symbol, the context needs to be considered to avoid confusion.

The above is the detailed content of What does the $ symbol mean in js. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template