简体   繁体   中英

Search and Delete / Regex / Ruby

I have a input where the User can enter his Code. The text_area input would look something like this

// ==UserScript== // @name test.org Scam Filter // @description Filters out scam scripts at Userscripts.org. // @namespace http://test.org:8080/scripts/review/163038 // @icon http://s3.test.com/uso_ss/icon/163038/large.png?1365299642 // @updateURL https://test.com/89.meta.js?c // @downloadURL https://test.com/89.user.js?c // @homepageURL https://test.com/code.php?id=89 // @require https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js // @include http*://test.org:8080/ // @include http*://test.org:8080/scripts* // @include http*://test.org:8080/tags/* // @grant GM_getValue // @grant GM_setValue // @grant GM_listValues // @version 5.33 // ==/UserScript== $(document).ready(function(){ // Load jQuery cookie functions and viewport visibility selectors loadJQcookies(); loadJQViewport(); /* The following 5 arrays comprise the blacklist.  etc etc etc

What i need is to delete from // ==UserScript== till // ==/UserScript== , but can't seem to find it.

for finding that specific code_block i use this regular expression:

source.scan( /\A\/\/\s==UserScript==(\w|\W)*\/\/\s==\/UserScript==$/ )

But i can't find a way to delete it..

I'm trying to run this in my model as a def delete_block and call before_create on it..

I tried

self.source.gsub(/\A\/\/\s==UserScript==(\w|\W)*\/\/\s==\/UserScript==$/, '' )

But i think i'm missing the logic here.

How can i achieve this ?

You problem is with the $ in the end. $ denotes "end of string". Since you string does not end with UserScript== , the match fails, and you find nothing:

self.source.gsub(/\A\/\/\s==UserScript==(\w|\W)*\/\/\s==\/UserScript==/, '' )
self.source
# => " $(document).ready(function(){ // Load jQuery cookie functions and viewport visibility selectors loadJQcookies(); loadJQViewport(); /* The following 5 arrays comprise the blacklist.  etc etc etc" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM