JS takes the most occurrences of the letters in a string

Source: Internet
Author: User
var str =adadfdfseffserfefsefseeffffftsdg; Name a variable to place the given string var maxLength = 0; Name a variable to place the highest number of occurrences of the letter and initialize it to 0var result = '; Name a variable to place the result input while (str! = ") {//loop iteration begins and determine if the string is empty oldstr = str;//assigns the original string variable to the new variable getstr = STR.SUBSTR (0,1);//Sub with string Str method gets the first character (first letter) of eval (str = str.replace (/+getstr+/g, ')); Details such as supplemental if (Oldstr.length-str.length  maxLength) {//Determine the length of the original string minus the substitution after the string length is greater than the maximum string length that occurred before maxLength = Oldstr.len Gth-str.length; Subtract two string lengths to get the maximum string length result = getstr + = + maxLength//returns the maximum string result (letters, occurrences)
}
}
Alert (Result)//Popup results
Supplement: eval (str = str.replace (/+getstr+/g, ')); maybe a lot of people want to write str = str.replace (/getstr/g, "), but the result will be wrong. Why, in this sentence, the expression matches the Getstr string, not the first letter that the getstr points to. By using the Eval method, you can avoid (first getstr get the first letter that you pointed to, connect str = str.replace (/+getstr+/g, ") with a string, and finally execute the code in eval, that is, the JavaScript code is interpreted before it is executed).

JS takes the most occurrences of the letters in a string

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.