| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/String/includes | [Back] [Original] |
Get to know MDN better
Cette page a t traduite partir de l'anglais par la communaut. Vous pouvez contribuer en rejoignant la communaut francophone sur MDN Web Docs.
Cette fonctionnalit est bien tablie et fonctionne sur de nombreux appareils et versions de navigateurs. Elle est disponible sur tous les navigateurs depuis septembre 2015.
La mthode includes() dtermine si une chane de caractres est contenue dans une autre et renvoie true ou false selon le cas de figure.
const sentence = "The quick brown fox jumps over the lazy dog.";
const word = "fox";
console.log(
`The word "${word}" ${
sentence.includes(word) ? "is" : "is not"
} in the sentence`,
);
// Expected output: "The word "fox" is in the sentence"
str.includes(chaneRecherche);
str.includes(chaneRecherche, position);
chaneRechercheUne chane rechercher dans la chane courante.
position FacultatifLa position dans la chane partir de laquelle commencera la recherche. La valeur par dfaut de position est 0.
true si la chane de caractres contient la sous-chane recherche, false sinon.
Cette mthode dtermine si une chane de caractres est contenue dans une autre.
includes() est sensible la casse. Par exemple, l'expression suivante nous retournera false :
"Baleine bleue".includes("baleine"); // false
includes()const str = "tre ou ne pas tre, telle est la question.";
console.log(str.includes("tre")); // true
console.log(str.includes("question")); // true
console.log(str.includes("plonasme")); // false
console.log(str.includes("tre", 1)); // false
console.log(str.includes("TRE")); // false
console.log(str.includes("")); // true
Cette mthode a t ajoute la spcification ECMAScript 2015 et n'est peut-tre pas encore disponible dans toutes les implmentations JavaScript.
Cependant, vous pouvez facilement polyfill cette mthode pour de vieux navigateurs :
if (!String.prototype.includes) {
String.prototype.includes = function (search, start) {
"use strict";
if (search instanceof RegExp) {
throw TypeError("first argument must not be a RegExp");
}
if (start === undefined) {
start = 0;
}
return this.indexOf(search, start) !== -1;
};
}
| Spcification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-string.prototype.includes |
Array.prototype.includes()TypedArray.prototype.includes()String.prototype.indexOf()String.prototype.lastIndexOf()String.prototype.startsWith()String.prototype.endsWith()Cette page a t modifie le 17 fvr. 2025 par les contributeurices du MDN.
StringString.prototype.anchor()String.prototype.at()String.prototype.big()String.prototype.blink()String.prototype.bold()String.prototype.charAt()String.prototype.charCodeAt()String.prototype.codePointAt()String.prototype.concat()String.prototype.endsWith()String.prototype.fixed()String.prototype.fontcolor()String.prototype.fontsize()String.prototype.includes()String.prototype.indexOf()isWellFormed()String.prototype.italics()String.prototype.lastIndexOf()String.prototype.link()String.prototype.localeCompare()String.prototype.match()String.prototype.matchAll()String.prototype.normalize()String.prototype.padEnd()String.prototype.padStart()String.prototype.repeat()String.prototype.replace()String.prototype.replaceAll()String.prototype.search()String.prototype.slice()String.prototype.small()String.prototype.split()String.prototype.startsWith()String.prototype.strike()String.prototype.sub()String.prototype.substr()String.prototype.substring()String.prototype.sup()String.prototype.toLocaleLowerCase()String.prototype.toLocaleUpperCase()String.prototype.toLowerCase()String.prototype.toString()String.prototype.toUpperCase()toWellFormed()String.prototype.trim()String.prototype.trimEnd()String.prototype.trimStart()String.prototype.valueOf()String.prototype[@@iterator]()Object/FunctionObject.prototype.__defineGetter__()Object.prototype.__defineSetter__()Object.prototype.__lookupGetter__()Object.prototype.__lookupSetter__()Object.prototype.hasOwnProperty()Object.prototype.isPrototypeOf()Object.prototype.propertyIsEnumerable()Object.prototype.toLocaleString()Object.prototype.toString()Object.prototype.valueOf()Certaines parties de ce contenu sont protges par le droit d'auteur 19982026 des contributeurs individuels de mozilla.org. Contenu disponible sous une licence Creative Commons.
| Web Proxy Viewer | New URL | Original Page |