The $ symbol in JavaScript represents a wildcard character that can match any character or the end of a specific string. Specific applications include: 1. Regular expression line end anchor; 2. Alias for selecting and operating elements in the jQuery library; 3. Alias for custom functions in some frameworks.
$ Meaning in JavaScript
In JavaScript, the $ symbol is a wildcard character, which means it can match any characters (including newlines).
Specific usage:
-
Regular expression: $ is usually used as the end-of-line anchor point in regular expressions, indicating regular expressions The expression pattern must match the end of the string. For example:
<code class="javascript">const re = /a$/;
re.test("apple"); // true
re.test("apples"); // false</code>
Copy after login
-
jQuery: The $ symbol is an alias for the jQuery library used to select and manipulate web page elements. For example:
<code class="javascript">$("p").text("Hello, world!"); // 更改所有 <p> 元素的文本</code>
Copy after login
-
Custom function: In some JavaScript frameworks, the $ symbol can be used as an alias for a custom function, depending on how the framework is implemented. . For example, in Vue.js, the $ symbol represents a Vue instance.
It’s worth noting: The
- $ symbol is not a reserved word in JavaScript, which means it can be reassigned or used other purposes.
- In strict mode, the $ symbol cannot be reassigned in regular expressions.
- In jQuery and other libraries, the usage of the $ symbol may vary depending on the specific API of the library.
The above is the detailed content of What does $ in js mean?. For more information, please follow other related articles on the PHP Chinese website!