-1

I need to match a string to have words and not numbers, I need to match special characters such as $!:{}_ if they are part of the word but ignore otherwise.

I have a regex that matches for word and ignores numbers but cannot work out how to match special characters if they are part of the word but ignore otherwise.

Here is what I have correctly - /^d\s+/

Any help would be appreciated.

Tushar
  • 85,780
  • 21
  • 159
  • 179
user2025749
  • 251
  • 1
  • 2
  • 11
  • 2
    http://stackoverflow.com/questions/13946651/matching-special-characters-and-letters-in-regex – Mike Sav Nov 18 '15 at 09:24
  • 2
    what do you mean by `match special characters if they are part of the word but ignore otherwise.` – M.kazem Akhgary Nov 18 '15 at 09:26
  • If you use any online regex tester, you will see that [`^d\s+`](https://regex101.com/r/dN8iM5/1) makes very little sense given your requirements. – Wiktor Stribiżew Nov 18 '15 at 09:30
  • Can include example string at Question? – guest271314 Nov 18 '15 at 09:33
  • Example string: This is a test of 1,2,3 word-count - test. So in this example I would expect it to match: This is a test of word-count test it should ignore 1,2,3 and the - that is on its own as it is not part of a word. – user2025749 Nov 18 '15 at 09:34
  • Then try `/\b[a-z]+(?:-[a-z]+)\b/ig`. – Wiktor Stribiżew Nov 18 '15 at 09:45
  • I have now been given a proper brief: Allow words and letters only, and ignore any numbers. Special characters such as (-_‘[]{}“£$&%!:;\/) should either be ignored or treated as part of the word they sit within. – user2025749 Nov 18 '15 at 09:59

1 Answers1

0

Allow words and letters only, and ignore any numbers. Special characters such as (-_‘[]{}“£$&%!:;/) should either be ignored or treated as part of the word they sit within.

Try using String.prototype.replace() with RegExp /\s\d+.*\d|\s+[^a-z]/ig to replace space character followed by digit followed by any character followed by digit , or space character followed by any character not a-z case insensitive

var str = "This is a test of 1,2,3 word-count - test.";

str = str.replace(/\s\d+.*\d|\s+[^a-z]/ig, "");

document.body.textContent = str;
guest271314
  • 1
  • 15
  • 104
  • 177