One option is to use regular expressions:
if (str.match("^Hello")) {
// do this if begins with Hello
}
if (str.match("World$")) {
// do this if ends in world
}
Answer from Lukáš Lalinský on Stack OverflowMDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › startsWith
String.prototype.startsWith() - JavaScript - MDN Web Docs
The characters to be searched for at the start of this string. Cannot be a regex. All values that are not regexes are coerced to strings, so omitting it or passing undefined causes startsWith() to search for the string "undefined", which is rarely what you want.
jQuery
api.jquery.com › attribute-starts-with-selector
Attribute Starts With Selector [name^=”value”] | jQuery API Documentation
Description: Selects elements that have the specified attribute with a value beginning exactly with a given string · attribute: An attribute name
Can I Use
caniuse.com › mdn-javascript_builtins_string_startswith
JavaScript built-in: String: startsWith | Can I use... Support tables for HTML5, CSS3, etc
"Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and mobile web browsers.
Top answer 1 of 6
407
One option is to use regular expressions:
if (str.match("^Hello")) {
// do this if begins with Hello
}
if (str.match("World$")) {
// do this if ends in world
}
2 of 6
104
For startswith, you can use indexOf:
if(str.indexOf('Hello') == 0) {
...
ref
and you can do the maths based on string length to determine 'endswith'.
if(str.lastIndexOf('Hello') == str.length - 'Hello'.length) {
Scaler
scaler.com › home › topics › javascript string startswith()
JavaScript String startsWith() Method - Scaler Topics
March 4, 2024 - The startsWith() method takes two parameters: searchString and position.
GeeksforGeeks
geeksforgeeks.org › jquery › how-to-check-a-string-starts-ends-with-a-specific-string-in-jquery
How to check a string starts/ends with a specific string in jQuery ? - GeeksforGeeks
July 12, 2025 - </title> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script> </head> <body> <h1 id="text"> Welcome To GEEKS FOR GEEKS!!! </h1> <h2> Check whether a word is present in the above sentence </h2> <input type="text" id="startkey"> <input type="text" id="endkey"> <button onclick="MyFunction()"> Check </button> <h2> startsWith() and endsWith() method </h2> <h2 id="startswith"> </h2> <h2> indexOf() method </h2> <h2 id="indexOf"></h2> <h2> search() method </h2> <h2 id="search"></h2> <h2> substring() method </h2> <h2 id="substring"></h2> <h2> substr() method </h2>
TutorialsPoint
tutorialspoint.com › article › how-to-check-a-string-starts-ends-with-a-specific-string-in-jquery
How to Check a String Starts/Ends with a Specific String in jQuery?
July 19, 2023 - For checking if strings start or end with specific characters, use startsWith() and endsWith() methods as they are specifically designed for this purpose and offer the best performance.
JavaScript Tutorial
javascripttutorial.net › home › javascript string methods › string.prototype.startswith()
JavaScript String startsWith() Method
November 3, 2024 - The startsWith() returns true if a string starts with a substring or false otherwise.
Jquery
forum.jquery.com › portal › en › community › topic › jquery-startswith-endswith
[jQuery] startsWith / endsWith
Unable to process your request · Sorry! Your request cannot be processed right now. Please try again later · Go to home page
Code.mu
code.mu › en › javascript › manual › string › startsWith
The startsWith method - checking the start of a string in JavaScript
The startsWith method checks if a string starts with a specified substring in JavaScript.
TechOnTheNet
techonthenet.com › js › string_startswith.php
JavaScript: String startsWith() method
Optional. It is the position within string that the search will begin. If this parameter is not provided, the startsWith() method will default to 0 and start the search at the beginning of the string.
Telerik
telerik.com › javascript › ui › filter › operators.string.startswith
operators.string.startswith - API Reference - Kendo UI Filter - Kendo UI for jQuery
<div id="filter"></div> <script> var data = [ { name: "Jane Doe", age: 30 }, { name: "John Doe", age: 33 } ]; var dataSource = new kendo.data.DataSource({ data: data }); $("#filter").kendoFilter({ dataSource: dataSource, operators: { string: { startswith: "Begins with" } }, fields: [ { name: "name", type: "string", label: "Name" } ] }); </script>
Codecademy
codecademy.com › docs › javascript › strings › .startswith()
JavaScript | Strings | .startsWith() | Codecademy
August 8, 2023 - The .startsWith() JavaScript string method checks whether a string begins with the specified characters.
W3Schools
www-db.deis.unibo.it › courses › TW › DOCS › w3schools › jsref › jsref_startswith.asp.html
JavaScript String startsWith() Method
Note: The startsWith() method is case sensitive. ... Color Converter Google Maps Animated Buttons Modal Boxes Modal Images Tooltips Loaders JS Animations Progress Bars Dropdowns Slideshow Side Navigation HTML Includes Color Palettes Code Coloring ... Your message has been sent to W3Schools. HTML Tutorial CSS Tutorial JavaScript Tutorial W3.CSS Tutorial Bootstrap Tutorial SQL Tutorial PHP Tutorial jQuery Tutorial Angular Tutorial XML Tutorial
Daily Dev Tips
daily-dev-tips.com › posts › javascript-startswith-and-multiple-conditions
JavaScript startsWith and multiple conditions
December 1, 2021 - How to use startsWith to check for multiple starting strings in JavaScript