user647345
user647345

Reputation: 143

Using gsub to strip multiple characters

I have an object which contains the following string "XXXXyyyy!!!!zzzz"

XXXX - never the same, will always change, it may be a million characters long
yyyy - will never change, exact number of characters all the time
!!!! - this is the data I want to result with after I clean the string. This data will always change as well
zzzz - will never change, exact number of characters all the time

so if:

string = "XXXXyyyy!!!!zzzz"  
string.gsub("zzzz","")  
# => "XXXXyyyy!!!!"

What can I do next to get just "!!!!". How can I use 'yyyy', which is static, to erase "XXXXyyyy" and leave only "!!!!". I hope this explains my question. Sorry about the last one.

Upvotes: 1

Views: 886

Answers (2)

seph
seph

Reputation: 6076

From your first attempt, if you change it to:

string = "if(location.hostname.indexOf( 'edition.' ) > -1) {document.write('May 8, 2011 -- updated 1854 GMT (0254 HKT)');} else {document.write('May 8, 2011 2:54 p.m. EDT');}"

string.gsub(/^.+else.+'(.+)'.+$/, $1)

Should do it.

Upvotes: 1

sawa
sawa

Reputation: 168091

You should do it in once:

string[/yyyy(.*)zzzz\z/, 1]  

Upvotes: 3

Related Questions