How to find a singular word that has a capital letter in Node.js ?
Singular Word: A singular noun is a noun that refers to only one person, place, thing, or idea. It’s contrasted with plural nouns, which refer to more than one person, place, thing, or idea. An example of a singular noun is programmer, which represents one programmer; an example of a plural noun is programmers, which represents two or more programmers.
To find a singular word that has a capital letter, first, we need to find the words that have a capital letter and then check whether these capital words are singular or not
Regex: Regex, short for “regular expression,” is a tool used to search and match patterns in text. It is commonly used in programming languages to search and manipulate strings. Regex can be used to find specific words or characters, as well as to make sure that a string follows a certain format, such as an email address or phone number.
Find Words: To find the words that have a capital letter, we can use the match() function of the String object. This function searches a string for a match against a regular expression and returns an array containing the search results.
Words with capital letter in string : Peoples said that, John is good boy [ 'Peoples', 'John' ]
Determine Singular or Plural Form:
- To determine the singular or plural form of a word. we are using pluralize package which uses advanced techniques such as machine learning to try to determine the correct form of the word.
Setup to Create Project: Follow the below steps to create a project application:
Step 1: Inside a new folder create a new project, using the command
npm init -y
Step 2: Install-module pluralize
npm install pluralize --save
Step 3: Create an index.js file
Project Structure: This is how the project structure should look like:
Example 1: In the below code regex is used to match all the words that start with a capital letter in the given string. The match() method is then used to find all the matches of this regular expression in the given string, which results in an array of words that start with a capital letter. The code then checks if each of these words is in the singular form using the isSingular() method of the “pluralize” library and adds it to the singulars array if it is singular.
string : "Geek is a programmer"
- index.js file:
Steps to run the application: Write the below code in the terminal to run the application:
Please Login to comment...