jayko03
jayko03

Reputation: 2481

javascript regex after specific text

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(.*)/

Upvotes: 0

Views: 109

Answers (2)

User863
User863

Reputation: 20039

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

Upvotes: 0

Akash Shrivastava
Akash Shrivastava

Reputation: 1365

you can replace using regex

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

Upvotes: 1

Related Questions