Two common methods for JS to determine the number of occurrences of a character in another string: js2

Source: Internet
Author: User

Two common methods for JS to determine the number of occurrences of a character in another string: js2

This document describes two common methods for determining the number of occurrences of a character in another string in JavaScript. We will share this with you for your reference. The details are as follows:

Two methods are provided for search verification.

1. Obtain the length principle by splitting

Var s = 'www .jb51.net'; var n = (s. split ('.'). length-1; alert (n); // pop up 2

2. Regular Expression

Function patch (re, s) {re = eval ("/" + re + "/ig") return s. match (re ). length;} var s = 'tools .jb51.net'; alert (patch ('T', s); // pop up 2

PS: For Regular Expression operations, here we provide two very convenient online tools for your reference:

JavaScript Regular Expression online testing tool:
Http://tools.jb51.net/regex/javascript

Regular Expression generation tool:
Http://tools.jb51.net/regex/create_reg

In addition, we recommend two easy-to-use online character statistics tools for your reference:

Online word count statistics tools:
Http://tools.jb51.net/code/zishutongji

Online character statistics and editing tools:
Http://tools.jb51.net/code/char_tongji

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.