0

I want to pull out only the text part(not special character !@#$%^&*()...)
In this case, I want to pull javascriptbye which can change, but for is stable.

var phrase = "helloworldpython2000forjavascript)bye";
var myPattern = /for(.*)/

myPattern pulls out all texts, javascript)bye. I tried /for(.[a-zA-Z]+$/ but no luck. How can I change it, so that I can have text after specific word?

Thanks!
EDIT
phrase could include space as well.

var phrase = "hello world python 2000 for javascript ) bye";
var myPattern = /for(.*)/
2
  • What method are you using? search, match, exec? Commented Dec 18, 2019 at 14:13
  • @empiric I am using match Commented Dec 18, 2019 at 14:18

2 Answers 2

1

you can replace using regex

var phrase = "helloworldpython2000forjavascript)bye";
console.log(phrase.replace(/.*for?/, "").replace(/[!@#$&()\\-`.+,\/\"]/, ""))

Sign up to request clarification or add additional context in comments.

Comments

0

Using Positive Lookbehind

/(?<=for.*)[\w]+/g

Demo

var phrase = "helloworldpython2000forjavascript)bye";
var myPattern = /(?<=for.*)[\w]+/g

console.log(phrase.match(myPattern))

https://caniuse.com/#feat=js-regexp-lookbehind

2 Comments

It works well even with space. Does yours take care space?
@jayko03 \w is [a-zA-Z0-9_]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.