| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/String/search | [Back] [Original] |
Get to know MDN better
Esta pgina ha sido traducida del ingls por la comunidad. Aprende ms y nete a la comunidad de MDN Web Docs.
This feature is well established and works across many devices and browser versions. Its been available across browsers since julio de 2015.
El mtodo search() ejecuta una bsqueda que encaje entre una expresin regular y el objeto String desde el que se llama.
str.search(expresionregular)
Un objeto expresin regular. Si se pasa un objeto obj que no sea una expresin regular, se convierte implicitamente en una expresin regualar usando new RegExp(obj).
El ndice de la primera coincidencia entre la expresin regular y la cadena de texto proporcionada, si no se encuentra devuelve -1.
Cuando se desee saber en qu casos un patrn se encuentra en una cadena de texto utiliza search() (si slo deseas saber si existe, utiliza el mtodo test() del prototipo de RegExp); para ms informacin (pero de ejecucin ms lenta) utiliza match() (similar al mtodo exec() de las expresiones regulares).
search()El siguiente ejemplo imprime un mensaje dependiendo de cul sea el resultado de la evaluacin.
function testinput(re, str) {
var midstring;
if (str.search(re) != -1) {
midstring = " contains ";
} else {
midstring = " does not contain ";
}
console.log(str + midstring + re);
}
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-string.prototype.search |
This page was last modified on 17 dic 2024 by MDN contributors.
StringString.prototype.anchor()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()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()trimStart()String.prototype.valueOf()[Symbol.iterator]()Object/FunctionYour blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |