Use javascript to obtain the most frequently used letter _ javascript skills in a string

Source: Internet
Author: User
The String "adadfdfseffserfefsefseetsdg" is used to find the most frequently used letters and the number of times. The typical solution is to retrieve the first character (first letter) of a string, and replace the regular expression (first character) with the replacement of null, the number of occurrences of this letter is the original string length minus the replacement string length. Find the longest letter in the loop iteration.

<Script type = "text/javascript"> var str = "adadadfdfseffserfefsefseeffffftsdg"; // name the string var maxLength = 0 given by a variable; // name the maximum number of times a variable is placed in a letter and initialized to 0 var result = ''; // name a variable and enter the result while (str! = '') {// Start of loop iteration, and judge whether the string is empty oldStr = str; // assign the original string variable to the new variable getStr = str. substr (); // obtain the first character (first letter) eval ("str = str. replace (/"+ getStr +"/g, '')"); // if (oldStr. length-str.length> maxLength) {// determine the length of the original string minus whether the length of the string after replacement is greater than the maximum length of the previous string maxLength = oldStr. length-str.length; // two string lengths subtract to get the largest String Length result = getStr + "=" + maxLength // return the largest string result (letters, occurrences )}} alert (result) // The result script is displayed.
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


Supplement:

The Code is as follows:


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


Many people may want to write str = str. replace (/getStr/g, ") in this way, but the result is wrong. Why? In this sentence, the expression matches the getStr string instead of the first letter pointed to by getStr. The eval method can be used to avoid this problem (first, getStr gets the first letter to which it points, and uses a string to connect "str = str. replace (/"+ getStr +"/g, ")", and finally execute this code in eval, that is, explain the Javascript code first, and then execute it ).

Because of the poor eval performance, error-prone and poor readability. We recommend that you change eval ("str = str. replace (/" + getStr + "/g,") ":
Str = str. replace (new RegExp (getStr, "g "),"")

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.