Open In App

How to find a singular word that has a capital letter in Node.js ?

Last Updated : 29 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside of the browser. It is widely used in developing APIs and microservices for small to large companies.

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.

Example 1: 

Javascript




const string = "Peoples said that, John is good boy";
console.log(`Words with capital letter in string : ${string}`);
const regex = /\b[A-Z][A-Za-z]*\b/g;
const result = string.match(regex);
console.log(result);


Output:

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:

Javascript




// require module which is used to determine
// the singular or plural form of a word
  
const pluralize = require("pluralize");
const string = "Geek is a programmer";
  
// regex is used to find all the words 
// which has capital letter in it
const regex = /\b[A-Z][A-Za-z]*\b/g;
const result = string.match(regex);
  
const singulars = [];
result.forEach((word) => {
    // isSingular method of pluralize library
    // to determine the singular form of a word
    if (pluralize.isSingular(word)) {
        singulars.push(word);
    }
});
  
console.log("Singular Words which has capital letter in it ");
console.log(singulars); // ['Geek']


Steps to run the application: Write the below code in the terminal to run the application:

node index.js

Output:

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads