Question

Je le code suivant


// this text box can contain a URL such as:
// http://vimeo.com/
// http://www.youtube.com/
// and so on.
var text = $('#myTextBox').val();
var providers = "youtube|flickr|viddler|qik|revision3|hulu|vimeo";

if( text.match(providers).length > -1) {  
  var selectedProvider = ???;
}

la méthode de correspondance semble s'il y a une sous-chaîne qui correspondent à la liste des fournisseurs: youtube, flickr, vimeo etc

Ma question est quel fournisseur a été adapté?

Était-ce utile?

La solution

Vous pouvez saisir le résultat de match et obtenir le premier élément adapté:

var text = $('#myTextBox').val();
var match = text.match("youtube|flickr|viddler|qik|revision3|hulu|vimeo");

if (match) {
  var selectedProvider = match[0];
}

String.prototype.match attend un objet RegExp comme argument, mais si vous passez un String, il sera remplacé par le résultat de l'expression new RegExp(string)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top