How to find a string, within a string, using Regex Ali Abbas on October 22, 2019 Thanks for stopping by. I am stuck in a problem that I'm unable to solve. The problem is like as follows a = "The Macports" b = "macport" c = "Th... Read full post Submit Preview Dismiss Collapse Expand SavagePixie SavagePixie SavagePixie Follow Always learning new things. I love web development and coding in general. Joined Jul 23, 2019 • Oct 22 '19 Dropdown menu Copy link Hide Do you have them in an array? That'd be ideal, as you could do something like this: const getWords = arr => { const regEx = /macports?/i return arr.filter(x => regEx.test(x)) } If no array, then forget about the filter, but you can still apply the same principle. Another way to go would be: string.includes("macport") Though you might have to turn the string into lower case for this one. Collapse Expand Saurabh Daware 🌻 Saurabh Daware 🌻 Saurabh Daware 🌻 Follow - JavaScript <3 - I make silly libraries - Building Abell, A new static-site-generator. Email saurabhdaware99@gmail.com Location Bengaluru, India 🇮🇳 Pronouns He/Him Work Senior Frontend Engineer @Razorpay Joined May 31, 2019 • Oct 22 '19 Dropdown menu Copy link Hide /macport$|macport /g.test(string) This will check for words having macport and a space appeneded or a string that ends with macport for macports you can do the same thing by replacing macport with macports I am on mobile so didn't check it much just check if it is working and I may have missed some cases Collapse Expand Saurabh Daware 🌻 Saurabh Daware 🌻 Saurabh Daware 🌻 Follow - JavaScript <3 - I make silly libraries - Building Abell, A new static-site-generator. Email saurabhdaware99@gmail.com Location Bengaluru, India 🇮🇳 Pronouns He/Him Work Senior Frontend Engineer @Razorpay Joined May 31, 2019 • Oct 22 '19 Dropdown menu Copy link Hide Oh I replied with the sense that you want to differentiate between macport and macports, is this what you want? Collapse Expand Ali Abbas Ali Abbas Ali Abbas Follow Tech enthusiast with the passion to develop applications that can make this world better. "Whenever I get bored, I call API's" Email irizviali@gmail.com Location India Education CS Engineer Work SDE-3 @ Trential - Building Decentralised Digital Identity | Full Stack Engineer Joined Apr 14, 2019 • Oct 22 '19 Dropdown menu Copy link Hide No, i want to include macport and macports both. Saurabh Daware 🌻 Saurabh Daware 🌻 Saurabh Daware 🌻 Follow - JavaScript <3 - I make silly libraries - Building Abell, A new static-site-generator. Email saurabhdaware99@gmail.com Location Bengaluru, India 🇮🇳 Pronouns He/Him Work Senior Frontend Engineer @Razorpay Joined May 31, 2019 • Oct 22 '19 Dropdown menu Copy link Hide oh then string.includes('macport') would work Collapse Expand SavagePixie SavagePixie SavagePixie Follow Always learning new things. I love web development and coding in general. Joined Jul 23, 2019 • Oct 22 '19 Dropdown menu Copy link Hide Oh, that's a very good point. It does change the answer. Collapse Expand Si Si Si Follow Husband & Father. Founder, Owner, Lead Developer, Writer and Reviewer of Code Tips. Senior Full Stack Software Developer. Location London Work Senior Fullstack Software Developer Joined May 23, 2019 • Oct 22 '19 Dropdown menu Copy link Hide Can you update the title of your post to better indicate what your issue is, so people searching may find the issue easier? Collapse Expand Ali Abbas Ali Abbas Ali Abbas Follow Tech enthusiast with the passion to develop applications that can make this world better. "Whenever I get bored, I call API's" Email irizviali@gmail.com Location India Education CS Engineer Work SDE-3 @ Trential - Building Decentralised Digital Identity | Full Stack Engineer Joined Apr 14, 2019 • Oct 22 '19 Dropdown menu Copy link Hide Okay Sure Collapse Expand Si Si Si Follow Husband & Father. Founder, Owner, Lead Developer, Writer and Reviewer of Code Tips. Senior Full Stack Software Developer. Location London Work Senior Fullstack Software Developer Joined May 23, 2019 • Oct 22 '19 Dropdown menu Copy link Hide I was thinking something more like: How to find a string, within a string, using Regex or something that actually summarises the issue you're wanting help for. "Can you help me" doesn't really give an initial indication of what's being asked. Ali Abbas Ali Abbas Ali Abbas Follow Tech enthusiast with the passion to develop applications that can make this world better. "Whenever I get bored, I call API's" Email irizviali@gmail.com Location India Education CS Engineer Work SDE-3 @ Trential - Building Decentralised Digital Identity | Full Stack Engineer Joined Apr 14, 2019 • Oct 22 '19 Dropdown menu Copy link Hide Okay! Done Collapse Expand Maynard Cabalitan Maynard Cabalitan Maynard Cabalitan Follow Ruby on rails dev Location Jasoom Education BS Computer Science Work Ruby Developer at Webcoder PH Joined Apr 30, 2019 • Oct 22 '19 Dropdown menu Copy link Hide in ruby if checking per string "The fastest macport".include?('macport') Code of Conduct • Report abuse
Do you have them in an array? That'd be ideal, as you could do something like this:
If no array, then forget about the
filter, but you can still apply the same principle.Another way to go would be:
Though you might have to turn the string into lower case for this one.
/macport$|macport /g.test(string)
This will check for words having macport and a space appeneded or a string that ends with macport
for macports you can do the same thing by replacing macport with macports
I am on mobile so didn't check it much just check if it is working and I may have missed some cases
Oh I replied with the sense that you want to differentiate between macport and macports, is this what you want?
No, i want to include macport and macports both.
oh then string.includes('macport') would work
Oh, that's a very good point. It does change the answer.
Can you update the title of your post to better indicate what your issue is, so people searching may find the issue easier?
Okay Sure
I was thinking something more like:
or something that actually summarises the issue you're wanting help for. "Can you help me" doesn't really give an initial indication of what's being asked.
Okay! Done
in ruby if checking per string