Thursday, February 23, 2023
HomeSoftware DevelopmentJavaScript Strategies for Looking Strings

JavaScript Strategies for Looking Strings

JavaScript tutorials

Must test whether or not a JavaScript string accommodates a particular character or substring? Or possibly you should know the place it seems within the string. In any occasion, JavaScript affords a number of methods to perform each these goals. This internet growth tutorial will current an summary of the String’s many strategies in addition to some examples of use the string object’s strategies devoted to discovering a substring inside one other string.

Learn: High HTML and CSS On-line Programs for Internet Builders

JavaScript String Strategies

Check out the total record of string strategies under and you’ll discover that there are extra strategies for looking out than some other function, which highlights their significance:


  • search(): searches for specified worth within the string
  • indexOf(): returns the primary index of prevalence of a worth
  • lastIndexOf(): returns the final index of prevalence of a worth
  • contains(): checks if given string is discovered inside a string
  • startsWith(): checks if a string begins with a specified string
  • endsWith(): checks if a string ends with a specified string
  • match(): returns results of matching string with a regex
  • matchAll(): returns iterator of outcomes matching with a regex


  • trim(): removes whitespace from each ends of a string
  • trimStart(): removes whitespace from the start of a string
  • trimEnd(): removes whitespace from the tip of a string


  • padStart(): pads a string firstly to a given size
  • padEnd(): pads a string on the finish to a given size


  • break up(): returns the string divided into record of substring
  • substring()/substr(): return a specified a part of the string
  • slice(): extracts and returns a bit of the string


  • concat (): concatenates the arguments to the calling string


  • substitute(): replaces a substring/sample within the string
  • replaceAll(): returns string by changing all matching patterns

Altering Case

  • toUpperCase(): returns uppercase of a string
  • toLowerCase(): returns lowercase illustration of a string

Characters and Unicode

  • charAt(): returns character at a specified index in string
  • charCodeAt(): returns Unicode of the character at given index
  • fromCharCode(): returns a string from the given UTF-16 code items
  • codePointAt(): returns the Unicode level worth at given index
  • fromCodePoint(): returns a string utilizing the given code factors

Miscellaneous JavaScript String Search Strategies

  • localeCompare(): compares two strings within the present locale
  • repeat(): returns a string by repeating it given occasions

Learn: Finest On-line Programs to Be taught JavaScript

Working with the JavaScript String Object’s Search Strategies

On this part of our JavaScript String methodology tutorial, we’ll go over every of the strategies listed within the Looking class above and canopy their syntax and utilization.


The search() methodology matches a string towards an everyday expression or string searchValue. Within the case of the latter, the string is transformed to an everyday expression by the strategy earlier than continuing with the search. The search() methodology returns the index (place) of the primary match (ranging from 0) or -1 if no match is discovered. The search() methodology is case delicate until the i flag is included within the common expression.

Syntax of search() Technique in JavaScript

Examples of search() Technique in JavaScript

let textual content = "Please find the place 'find' happens!";
// 1st occasion of "find"
doc.writeln(textual"find")); // 7

textual content="The fast brown fox jumps over the lazy canine. If the canine barked, was it actually lazy?";
// Any character that's not a phrase character or whitespace
doc.writeln(textual[^ws]/g)); // 43

textual content = "Mr. Blue has a blue home";
// Case insensitive seek for "blue"
doc.writeln(textual; // 4

indexOf()/lastIndexOf() Technique in JavaScript

The indexOf() and lastIndexOf() strategies return the index of (place of) the primary and final prevalence of a string in a string respectively or -1 if no match is discovered.

Syntax of indexOf()/lastIndexOf() Technique


Examples of indexOf()/lastIndexOf() Technique

let textual content = "Please find the place 'find' happens!";
textual content.indexOf("find");     // 7
textual content.lastIndexOf("find"); // 21
textual content.lastIndexOf("textual content");   // -1 (not discovered)

The Distinction Between search() and indexOf()

The search() and indexOf() strategies are strikingly related, albeit with a few notable variations:

  • The search() can’t take a begin place argument.
  • The indexOf() methodology can’t search utilizing an everyday expression.

contains() Technique in JavaScript

The contains() methodology returns true if a string accommodates a specified string worth. In any other case it returns false.

Syntax of contains() Technique


Examples of are() Technique

let textual content = "Please find the place 'find' happens!";
textual content.contains("find"); // true
textual content.contains("discover");   // false

Notice that the contains() methodology is an ES6 function and isn’t supported in Web Explorer.

startsWith()/endsWith() Technique in JavaScript

The startsWith() and endsWith() strategies return true if a string begins or ends with a specified worth, respectively. In any other case, false is returned.

Syntax of startsWith()/endsWith() Technique


Examples of startsWith()/endsWith() Technique

let textual content = "Please find the place 'find' happens!";
textual content.startsWith("Please"); // true
textual content.startsWith("find"); // false

textual content.endsWith("happens!"); // true
textual content.endsWith("find");  // false

Notice that the startsWith() and endsWith() strategies are an ES6 function and aren’t supported in Web Explorer.

match()/matchAll() Technique in JavaScript

Each the match() and matchAll() strategies return the outcomes of matching a string towards a provided string or common expression. The distinction is that match() returns the ends in an array whereas matchAll() returns an iterator. Launched in ES6, iterators will let you iterate over any object that follows the Iterator specification.

Syntax of match()/matchAll()


Examples of match()/matchAll()

let textual content = "Please find the place 'find' happens!";
textual content.match("cat"); // [cat]
textual content.match(/cat/g); // [cat, cat]

textual content.matchAll("happens!"); // Iterator with 1 end result
textual content.matchAll("find");  // Iterator with 2 outcomes

Notice that the matchAll() methodology is an ES2020 function and are positively not supported in Web Explorer.

Learn: High Collaboration Instruments for Internet Builders

Easy methods to Invoke a String Technique in JavaScript

The JavaScript string is a knowledge sort in contrast to some other in that it may be handled as both a primitive or object relying on whether or not it’s assigned as a string literal or created utilizing the String constructor. Does that imply that primitive Strings shouldn’t have entry to the String object’s many helpful strategies? By no means. Each time a technique is invoked on a primitive string, JavaScript will robotically wrap the string primitive and name the strategy on the wrapper object as a substitute. So, nonetheless you create your strings, you’ll be able to all the time entry their strategies.

You possibly can see all the strategies from this tutorial in motion within the codepen demo.

Last Ideas on JavaScript Strategies for Looking Strings

This internet growth tutorial introduced an summary of JavaScript’s String’s many strategies in addition to some examples of use them. Within the subsequent installment, we shall be having a look on the trimming, padding, extracting, and concatenating strategies of the String object. That can depart the remaining strategies for the third and ultimate installment.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments

situs slot gacor provider terbaik agen toto slot terpercaya 2023 agen toto togel terpercaya 2023 situs toto togel pasaran resmi terbaik bandar toto macau pasaran resmi toto togel bandar toto slot gacor 4d 2023 bo togel online pasaran terlengkap sepanjang masa bo toto slot terlengkap sepanjang masa situs toto togel 2023 bet 100 perak daftar toto slot dan toto togel 2023 bermain toto togel dengan bet hanya 100 perak daftar toto slot bonus new member terpercaya bermain toto slot pelayanan 24 jam nonstop agen slot gacor 4d hadiah terbesar bandar toto slot provider terbaik toto slot gacor 4d hingga toto togel toto togel pasaran resmi terpercaya bo togel online terbaik 2023 agen togel online terbesar 2023 situs togel online terpercaya 2023 bo togel online paling resmi 2023 toto togel pasaran togel hongkong resmi situs slot online pasti gacor agen slot online anti rungkad bo slot online deposit tanpa potongan situs toto togel dan toto slot bonus new member situs toto slot gacor 4d bo toto slot gacor 4d bo toto slot gacor dari toto togel 4d bo toto slot 4d terpercaya bo toto slot terpercaya toto macau resmi dari toto togel 4d agen togel terbesar dan situs toto slot terpercaya bandar toto togel dan slot online 2023 bo slot gacor terbaik sepanjang masa winsortoto winsortoto bo toto togel situs toto situs toto togel terpercaya situs toto slot terpercaya situs slot gacor 4d terbaik sepanjang masa agen toto togel dan situs toto slot terpercaya situs toto togel dan agen toto slot terpercaya bandar toto togel tersedia pasaran toto macau resmi agen toto togel bet 100 perak deposit 10rb ltdtoto