| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/String/search | [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 juillet 2015.
La mthode search() xecute une recherche dans une chaine de caractres grce une expression rationnelle applique sur la chane courante.
const paragraph = "I think Ruth's dog is cuter than your dog!";
// Anything not a word character, whitespace or apostrophe
const regex = /[^\w\s']/g;
console.log(paragraph.search(regex));
// Expected output: 41
console.log(paragraph[paragraph.search(regex)]);
// Expected output: "!"
str.search(regexp);
regexpUn objet reprsentant une expression rationnelle. Si l'objet pass n'est pas un objet d'expression rgulire, il est directement converti en une instance de RegExp en utilisant new RegExp(obj).
Si la recherche aboutit, search() renvoie un entier qui correspond l'indice de la premire correspondance trouve dans la chane. Si rien n'est trouv, la mthode renvoie -1.
Si la recherche est positive, search() renvoie l'indice de la premire correspondance pour l'expression rationnelle au sein de la chaine de caractres. Sinon, la mthode renvoie -1.
Si on souhaite savoir si un motif est trouv dans une chaine de caractres, on utilisera cette mthode (semblable la mthode test()) du prototype de RegExp ; pour plus d'informations (mais une xecution plus lente), on utilisera match() (semblable la mthode exec() pour les expressions rationnelles). La mthode test est semblable mais renvoie uniquement un boolen indiquant si une correspondance a t trouve.
Dans l'exemple suivant, on utilise une chane de caractres pour laquelle on applique deux expressions rationnelles (la premire permet d'obtenir une correspondance et la seconde n'en trouve aucune).
var maChaine = "CoucOu";
var regex1 = /[A-Z]/g;
var regex2 = /[.]/g;
console.log(maChaine.search(regex1)); // Renvoie 0, la position de la premire majuscule
console.log(maChaine.search(regex2)); // Renvoie -1 car il n'y a aucun point dans la chane
| Spcification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-string.prototype.search |
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 |