Use JS to realize the precise identification of palindrome number!!!

Source: Internet
Author: User

The author recently on one side read "JS Advanced programming 3" side on the FCC to find the topic of practice AH. It's called a cool one. This does not, just with life in the classroom, bedroom, laboratory, library and other places will be the fifth chapter "reference type" to do, the FCC will be discreet to the author to a "palindrome number", the author gnashing of Teeth, spent two days, is to study the array, is to study the string, is the study scope, but also looked at a long time Fortunately, do not bear, HEI hey, now for everyone to share in detail with JS to achieve accurate palindrome number identification!!!

Let's show you a few types of strings:

Race car

Not a palindrome

A man, plan a, canal a. Panama

My age is 0, 0 si ega ym.

0_0 (:/-\:) 0-0

Trouble, but also letters, and numbers, as well as underscores, spaces, points and so do not know what things ...

So, the first step we have to do is to get rid of all the numbers and letters !!!

Here, using the first method, the name is replace (), here we focus on sharing ideas, not clear replace () and some of the following other methods of the students themselves to query OH.

Well, our approximate idea is to replace (exp, ""). That means, match all the non-characters and then remove it.

So the question is, how does the regular expression match all the non-characters?

This is a problem that makes me want to break my head-.-

At the beginning, a friend suggested that I use \s later only to know that it matches the white space character, so some symbols are not matched to the

After my research on the regular expression, plus some discussion with my friends (oh ~), finally let me find a way

\w+ matches all non-characters

Because, \w match is the number, the letter, so \w is the antisense.

But it's not over yet, \w it can't match the underline, so we have to add \_+

Here, the problem is basically solved. Next, just use the method in the reference type to make sure it's a palindrome number.

Take a look at the following code ~

1<! DOCTYPE html>234<meta charset= "Utf-8"/>5<title></title>67<body>8<script type= "Text/javascript" >9         functionpalindrome (str) {Ten   //Good luck! One   varNewstr= str.replace (/\w+/g, ");//matches all non-word characters, replaced with an empty string ANewstr= Newstr.replace (/\_+/g, ");//match all underscores, replace with an empty string -Newstr=newstr.tolowercase (); -   //convert string to lowercase the   vararr1= Newstr.split ("); - Console.log (Arr1.reverse ()); -   varArr2=arr1.reverse (); -    +   varNewstr2=arr2.join (' '); -   //Refactoring a reversed string +  A   if(NEWSTR==NEWSTR2) {//Compare 1 at     return true; -}Else{ -    return false; -   } - } -Palindrome ("Not a palindrome"); in  -</script> to</body> +

Here is to remind everyone, pay attention to split ("), and more to see if their objects have achieved the desired effect of it ~

Sharing is over, thank you.

Use JS to realize the precise identification of palindrome number!!!

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.