plaidshirt
plaidshirt

Reputation: 5701

Get substring between 2 specific sequence of characters from text

I have a long text in which are several special characters, so I cannot filter to " or ) characters. Needed text and its position:

(Job:
"THIS TEXT")

I tried to get THIS TEXT with following code:

var jobstring = jobname.substring(jobname.indexOf("(Job: \"") + 7)
jobstring = jobstring.substring(0, jobstring.indexOf("\")"));

and this code too:

var jobstring=jobname.substring(jobname.lastIndexOf("Job: ")+7,jobname.lastIndexOf("")"));

None of those output is THIS TEXT, but I don't know exactly what I missed.

Upvotes: 0

Views: 96

Answers (2)

Cawet
Cawet

Reputation: 254

There is no space after job: but a ligne break. you'll have to use a regexp with \n \r, or \s witch is for all blank space.

    result = jobname.match(/Job:\s"(.+)"/g);

I suggest you to use a regexp tool, you will find many of them. https://duckduckgo.com/?q=regexp+tool&ia=web

Upvotes: 1

fafl
fafl

Reputation: 7387

Why not use regex?

var s = '(Job:\n"THIS TEXT")';
var rx = /Job:\s*"([^"]*)"/g
console.log(rx.exec(s)[1])

Upvotes: 1

Related Questions