locked
Syntax help RRS feed

  • Question

  • User-1753299103 posted

    I need help with the following syntax. It's not working the way I have it:

    @functions{
    
    	public static string CreateLinks(string s){
    
        		var rs = db.Query("select * from unwantedword");
         		foreach(var row in rs) {  
            		var unwanted = new Regex(@"(row.word\b)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            		s  = unwanted.Replace(s, "***");  
         		}
            	return s;
    
    	}
    
    }
    
    @Html.Raw(CreateLinks(row.comment)

    Thanks

    Friday, January 30, 2015 1:53 PM

Answers

  • User-735851359 posted

    what if you write that (using...) inside the functions?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 31, 2015 6:11 AM

All replies

  • User197322208 posted

    It's not working the way I have it:

    How it works and how do you want to work? Please give an example.

    Friday, January 30, 2015 2:39 PM
  • User-735851359 posted

    Hello,

    I do not understand the aim of the Regex here, but I think you want to find the unwanted words and change them with ***.

    change this:

    s  = unwanted.Replace(s, "***");

    to the following:

    s  = s.Replace(unwanted, "***");
    Friday, January 30, 2015 3:13 PM
  • User-1753299103 posted

    For some reason I am getting the following error:

    The name 'db' does not exist in the current context

    I have the connection at the top of my page and other queries are working.

    When I move the query outside the @function then it doesn't recognize "rs" in foreach(var row in rs) {

    Friday, January 30, 2015 3:56 PM
  • User197322208 posted

    The name 'db' does not exist in the current context

    I have the connection at the top of my page and other queries are working.

    please show

    Friday, January 30, 2015 7:46 PM
  • User-1753299103 posted
    @{
    
    	using(var db = Database.Open("mydatabase")){ 
    
    		@functions{
    
    			public static string CreateLinks(string s){
    
     	   			var rs = db.Query("select * from unwantedword");
     	    			foreach(var row in rs) {  
            				var unwanted = new Regex(@"(row.word\b)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            				s  = unwanted.Replace(s, "***");  
     	    			}
            			return s;
    
    			}
    		}
    
    
    		@Html.Raw(CreateLinks(row.comment))	
    
    	}
    
    }

    Friday, January 30, 2015 8:09 PM
  • User-735851359 posted

    what if you write that (using...) inside the functions?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 31, 2015 6:11 AM
  • User-1753299103 posted

    Yes that did it.

    Thanks

    Saturday, January 31, 2015 3:22 PM