Reputation: 29
I have text like this:
Some guy writes: some content.
or
Some guy said: some content.
and i want to get all content from 'writes' to the end.
It is looking simple: (said | writes):. *
But. My text example can looks like this:
Some guy writes: blablabla, said: some content
Here i want to get only 'said: some content'. But my regex geting all content from 'writes' to the end. How to solve this problem?
Upvotes: 3
Views: 65
Reputation: 6282
I don't think without defining what the string is splitting by you could possibly do it with RegExp. You really should solidify the pattern for your string and enforce it's use, but here is a programmatic way to parse your strings.
const writes = 'Some guy writes: some content.'
const said = 'Some guy said: some content.'
const blah = 'Some guy writes: blablabla, said: some content'
function objectifyString(str) {
const reg = /(said|writes):/
const index = str.search(reg) || 0
const parts = str.substring(index).split(reg).filter(item => item.length)
const obj = {}
const ll = parts.length
let ii = 0
for (; ii < ll; ii+=2) {
obj[parts[ii]] = (parts[ii+1]).trim()
}
return obj
}
console.log(
objectifyString(blah)
)
console.log(
[writes, said, blah].map(objectifyString)
)
Upvotes: 2
Reputation: 1610
here is your answer :
var text = "Some guy writes: blablabla, said: some content";
var output = /.*\s(\w+:.*)/.exec(text);
// output[1] contains your answer
Upvotes: 1
Reputation: 626853
Prepend the regex you have with ^.*
and capture it:
^.*((?:said|writes):.*)
See the regex demo
The initial .*
will grab the whole line (or string if DOTALL modifier is used) and will backtrack to accommodate for said:
or writes:
that are the last on the string/line.
The non-capturing group in (?:said|writes)
is used for grouping only purposes, so that there is only one capturing group with ID = 1.
Details:
^
- start of string/line anchor (depends on the modifiers used).*
- any 0+ chars as many as possible (as *
is a greedy quantifier) (other than line break symbols if DOTALL modifier is not used)((?:said|writes):.*)
- Group 1 capturing:
(?:said|writes)
- either said
or writes
char sequences:
- a colon.*
- any 0+ chars (other than line break symbols if DOTALL modifier is not used) up to the end of line/string.Upvotes: 4
Reputation: 36304
You can use the regex : ^.*(\b\w+:.*)$
The regex captures a group with pattern "<word>:<some content>$"
Upvotes: 1