How to check whether a string contains a substring in JavaScript?
ECMAScript 6 introduced String.prototype.includes
:
const string = "foo";
const substring = "oo";
console.log(string.includes(substring)); // true
Javascript check if string contains only certain character
Check this
<div class="container">
<form action="javascript:;" method="post" class="form-inline" id="form">
<input type="text" id="message" class="input-medium" placeholder="Message" value="Hello, world!" />
<button type="button" class="btn" data-action="insert">Show</button>
</form>
</div>
JavaScript
var onloading = (function () {
$('body').on('click', ':button', function () {
var a = document.getElementById("message").value;
var hasS = new RegExp("^[s\s]+$").test(a);
alert(hasS);
});
}());
Example http://jsfiddle.net/kXLv5/40/
How to check if a string only contains certain characters in js?
A regular expression with a character set of only those characters you want to permit would work:
const test = str => /^[a-z-]+$/.test(str);
console.log(
test('foo'),
test('Bar')
);
Check if a string has a certain piece of text
Here you go: ES5
var test = 'Hello World';
if( test.indexOf('World') >= 0){
// Found world
}
With ES6 best way would be to use includes
function to test if the string contains the looking work.
const test = 'Hello World';
if (test.includes('World')) {
// Found world
}
How to validate a string contains certain characters?
This regex will match a string containing only those characters:
^[+\-0-9(). ]+$
Working Demo
How do I check if string contains substring?
Like this:
if (str.indexOf("Yes") >= 0)
...or you can use the tilde operator:
if (~str.indexOf("Yes"))
This works because indexOf()
returns -1
if the string wasn't found at all.
Note that this is case-sensitive.
If you want a case-insensitive search, you can write
if (str.toLowerCase().indexOf("yes") >= 0)
Or:
if (/yes/i.test(str))
The latter is a regular expression or regex.
Regex breakdown:
/
indicates this is a regexyes
means that the regex will find those exact characters in that exact order/
ends the regexi
sets the regex as case-insensitive.test(str)
determines if the regular expression matchesstr
To sum it up, it means it will see if it can find the lettersy
,e
, ands
in that exact order, case-insensitively, in the variablestr
Validate if a string contains only some characters in javascript
A simple example:
var isValidString = stringToValidate.match(/^[AaBb>]*$/) !== null;
Explanation:
String.prototype.match() can be called on any string and it will return an array, if the regex matched (containing the matched substrings) or null.
Wrapping the regular expression between caret (^
) and dollar ($
) signs will ensure, that you check the string in full length.
You can add any characters between square brackets as you like, it will only match those.
Related Topics
How to Reload Datatables from Another Ajax Request With Onclick
What Does This Format Means T00:00:00.000Z
How to Check If a Textbox Is Empty Using JavaScript
How to Check If Input Field Is in Focus or Not
Scroll to Element from Typescript Code (Angular 5)
How to Pass Array Variable from View to Controller in Spring MVC
Select Li Element With Arrow Keys (Up and Down) Using JavaScript (Jquery)
Javascript Return True or Return False When and How to Use It
Discord.Js Deletemessage() Doesn't Work
Handling Multiple Http Responses One by One in Angular 6
Onclick Button Get from Database and Display on a Input Field Live
Clear an Input Field With Reactjs
How to Merge an Array of Objects With Same Keys in Es6 JavaScript
Vuejs Reload If in the Same Route
Node.Js Call Function If Button Clicked