Home >Web Front-end >JS Tutorial >Use JS to get the most frequent letters in a string_javascript skills

Use JS to get the most frequent letters in a string_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:05:501290browse

[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

Supplementary: The code is as follows:


eval("str = str.replace(/" getStr "/g, '')");


Many people may want to write str = str.replace(/getStr/g,"), but the result will be wrong. Why, in this sentence. The regular expression matches the getStr string, not the first letter pointed to by getStr. This can be avoided through the eval method (first getStr gets the first letter pointed to, and uses string concatenation "str = str.replace(/" getStr "/g, ")", and finally execute this code in eval, that is: interpret the Javascript code first, and then execute it).

Due to the poor performance of eval, it is error-prone and has poor readability. It is recommended to change eval(”str = str.replace(/” getStr ”/g,”)”) to: str = str.replace(new RegExp(getStr,"g"),"")
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