Gleydson S. Tavares Gleydson S. Tavares - 1 year ago 105
Javascript Question

How do I check if string contains a list of signs?

I hava a list of sings:

var sings = [",",".",":","!","?"]

How do I check if a word contains one of these signs and return it?

For example:


return ","


return "."


return "?"

Answer Source

You could solve that with a regular expression:

function match(input) {
  var regex = /([,\.\:!\?])/;
  var matches = input.match(regex);
  return matches ? matches[0] : false;

console.log(match("foo?"));   // "?"
console.log(match("bar."));   // "."
console.log(match("foobar")); // false

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download