How to Tell If a String Contains a Certain Character in JavaScript

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 regex
  • yes means that the regex will find those exact characters in that exact order
  • / ends the regex
  • i sets the regex as case-insensitive
  • .test(str) determines if the regular expression matches str
    To sum it up, it means it will see if it can find the letters y, e, and s in that exact order, case-insensitively, in the variable str

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



Leave a reply



Submit