Hướng dẫn javascript accented characters
In JavaScript I create a string which has accented characters such as 'ò' or 'à', e.g. "ò à" Show I seem to be unable to create text nodes in html which show the accented characters: if i pass them directly, i get "white question mark in black rhombus"; if i replace the accented characters by the corresponding html
entities, i get the entities printed verbatim.
Produces the following text:
How do I have to convert the string to get accented characters in HTML?
sdgluck 22.7k7 gold badges70 silver badges87 bronze badges asked Feb 22, 2015 at 13:16
2 You are probably missing a page encoding. Make sure your HTML includes either this:
Or this (HTML5):
EDIT It now seems that the problem is the encoding of your Javascript file. It too needs to be Unicode (encoded as UTF8 for example), as if it is saved in a non-Unicode encoding you are likely to have ò and à misinterpreted by the browser which might read these with a different encoding. Just open it in a text editor and save it as UTF8 (doing so will include a BOM at the start of the file that will specify its encoding, solving the problem) answered Feb 22, 2015 at 13:20
ClafouClafou 15.1k7 gold badges56 silver badges89 bronze badges 7 It turned out my xemacs encodes with ISO-8859-1; the current version can not be easily set to UTF-8. The solution was to change the encoding info from "utf-8" to "iso-8859-1" in the xhtml's header:
Now i can set a string to 'àò' in my js file, and the browser will display it correctly. answered Feb 23, 2015 at 19:21
user1479670user1479670 1,0473 gold badges10 silver badges22 bronze badges 1 Regular expressions are patterns used to match character combinations in strings. In JavaScript, regular expressions are also objects. These patterns are used with the
You construct a regular expression in one of two ways: Writing a regular expression pattern A regular expression pattern is composed of simple characters, such as Note: If you are already familiar with the forms of a regular expression, you may also read the cheatsheet for a quick lookup for a specific pattern/construct. Using simple patterns
Simple patterns are constructed of characters for which you want to find a direct match. For example, the pattern Using special characters When the search for a match requires something more than a direct match, such as finding one or more b's, or finding white space, you can include special
characters in the pattern. For example, to match a single The following pages provide lists of the different special characters that fit into each category, along with descriptions and examples. AssertionsAssertions include boundaries, which indicate the beginnings and endings of lines and words, and other patterns indicating in some way that a match is possible (including look-ahead, look-behind, and conditional expressions). Character classesDistinguish different types of characters. For example, distinguishing between letters and digits. Groups group multiple patterns as a whole, and capturing groups provide extra submatch information when using a regular expression pattern to match against a string. Backreferences refer to a previously captured group in the same regular expression. QuantifiersIndicate numbers of characters or expressions to match. Unicode property escapesDistinguish based on unicode character properties, for example, upper- and lower-case letters, math symbols, and punctuation. If you want to look at all the special characters that can be used in regular expressions in a single table, see the following: Escaping If you need to use any of the special characters literally (actually searching for a Similarly, if you're writing a regular expression literal and need to match a slash ("/"), you need to escape that (otherwise, it terminates the pattern). For instance, to search for the string "/example/" followed by one or more alphabetic characters, you'd use To match a literal backslash, you need to escape the backslash. For instance, to match the string
"C:\" where "C" can be any letter, you'd use If using the If escape strings are not already part of your pattern you can add them using
The "g" after the regular expression is an option or flag that performs a global search, looking in the whole string and returning all matches. It is explained in detail below in Advanced Searching With Flags. Why isn't this built into JavaScript? There is a proposal to add such a function to RegExp. Using parenthesesParentheses around any part of the regular expression pattern causes that part of the matched substring to be remembered. Once remembered, the substring can be recalled for other use. See Groups and backreferences for more details. Using regular expressions in JavaScriptRegular expressions are used with the When you want to know whether a pattern is found in a string, use the In the following example, the script uses the
If you do not need to access the properties of the regular expression, an alternative way of creating
(See Using the global search flag with If you want to construct the regular expression from a string, yet another alternative is this script:
With these scripts, the match succeeds and returns the array and updates the properties shown in the following table. As shown in the second form of this example, you can use a regular expression created with an object initializer without assigning it to a variable. If you do, however, every occurrence is a new regular expression. For this reason, if you use this form without assigning it to a variable, you cannot subsequently access the properties of that regular expression. For example, assume you have this script:
However, if you have this script:
The occurrences of Advanced searching with flagsRegular expressions have optional flags that allow for functionality like global searching and case-insensitive searching. These flags can be used separately or together in any order, and are included as part of the regular expression. To include a flag with the regular expression, use this syntax:
or
Note that the flags are an integral part of a regular expression. They cannot be added or removed later. For example,
You could replace the line: with:
and get the same result. The Using the global search flag with exec()
In contrast,
Using unicode regular expressionsThe "u" flag is used to create "unicode" regular expressions; that is, regular expressions which support matching against unicode text. This is mainly accomplished through the use of Unicode property escapes, which are supported only within "unicode" regular expressions. For example, the following regular expression might be used to match against an arbitrary unicode "word": There are a number of other differences between unicode and non-unicode regular expressions that one should be aware of:
Unicode regular expressions have different execution behavior as well.
ExamplesNote: Several examples are also available in:
Using special characters to verify inputIn the following example, the user is expected to enter a phone number. When the user presses the "Check" button, the script checks the validity of the number. If the number is valid (matches the character sequence specified by the regular expression), the script shows a message thanking the user and confirming the number. If the number is invalid, the script informs the user that the phone number is not valid. The regular expression looks for:
HTML
JavaScript
ResultRegExr An online tool to learn, build, & test Regular Expressions. Regex testerAn online regex builder/debugger Regex interactive tutorialAn online interactive tutorials, Cheatsheet, & Playground. Regex visualizerAn online visual regex tester.
|