Advertisement
badunius

Text to Dictionary (JS, RegEx)

Mar 27th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // building a dictionary
  2. var txt = 'It was the she-wolf who had first caught the sound of men\'s voices and the whining of the sled-dogs; and it was the she-wolf who was first to spring away from the cornered man in his circle of dying flame. The pack had been loath to forego the kill it had hunted down, and it lingered for several minutes, making sure of the sounds, and then it, too, sprang away on the trail made by the she-wolf.'
  3.  
  4. // gotta refine this expression, though
  5. // as it will catch `wolf--one` as a single word in `a large grey wolf--one of its several leaders`
  6. var reg = /\b(\S+)\b/gmi
  7.  
  8. // and find a way to merge two sets (update dictionary)
  9. var dic = new Set(
  10.     txt
  11.         .match(reg)
  12.         .map(w => w.toLocaleLowerCase())
  13.         .sort()
  14. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement