Results 1 to 3 of 3

Thread: replace fuction, espace single double quotes.

Hybrid View

  1. #1
    Join Date
    Jun 2007

    replace fuction, espace single double quotes.

    I am trying to remove single quotes and double quotes from a string of escaped characters,
    it works with all except for single quotes ' and double quotes "
    this is my code:

    var newstrchanged = strEscaped.replace(/^%22/g, ' '); //does not remove the double quotes

    var newstrchanged = strEscaped.replace(/^%27/g, ' '); //does not remove the single quote

    var newstrchanged = strEscaped.replace(/^%3A/g, ' '); //does remove the SEMI COLON %3A

    thank you for your help

  2. #2
    Join Date
    Feb 2013
    strEscaped is not changed in your code. So for example the first time u make newstrchanged it might have the value of strEscaped with no quotes. The second time u make newstrchanged, it will still have quotes, but might not have single-quotes. The reason it still has quotes is because strEscaped isn't changed. The third time newstrchanged should still have quotes and apostrophes, but shouldn't have colons (unicode 003A). So.. That won't work as you expect, but it works as you describe. Here

    Really, that regex shouldn't work to remove more than one thing. When you use the '^' as the first token, it means the string starts with. Are you trying to say "If the string starts with a quote, remove the quote? I think you meant to remove all quotes from the string- seeing as you used 'g' flag. In regular expressions you compare the unicode value using the '\uXXXX' token. So you want these regular expressions and this code:
    var patterns = {
      "quote": /\u0022/g,
      "singleQuote": /\u0027/g,
      "colon": /\u003a/g,
      "semi-colon": /\u003b/g
    var str = '"This is a test of what\'s expected":.. ;;;:';
    var newStr = str;
    for (var i in patterns) {
      newStr = newStr.replace(patterns[i], '');
    But really you don't -need- that much code. You can do it in one line:
    var newStr = str.replace(/[\u0022\u0027\u003a\u003b]/g, '');
    But, that's not very efficient or convenient. Storing this action in a function named 'escape' is worth-while. In addition, storing the regular expression in a variable will be more efficient.
    var escape = (function () {
        var regex = /[\u0022\u0027\u003a\u003b]/g;
        return function escape (str) {
            return str.replace(regex, '');
    Now there's an escape function. Here's how you would use it:
    var str = '"This is a test of what\'s expected":.. ;;;:';
    var newStr = escape(str);
    But that kind of escaping is known as "black-listing" which isn't very secure- as people can find ways to fill cracks that the blacklist didn't think of. White-listing is the opposite. A white-list is a list of allowed characters. For example, if a username only should have letters numbers and underscores, this is the regex to assure that:
    var not_legal = /[^a-z0-9_]/ig;
    var user = "$alpha_Omeg-a".replace(not_legal, '');
    // user is 'alpha_Omega'
    Note that '^' when used as the first character inside of the square-brackets [ ], then the '^' changes the character match to anything EXCEPT what is in the brackets [ ]. When '^' is used as the first character in a regular expression in general, though, it means the string must start with the following character(s) in order to match.
    We can drop the 'i' and the ranges because we can target 'word-characters' and 'digits' instead. \w contains upper-case letters, so we no longer need the 'i' (which means 'case-insensitive') if we choose to use the following regex to match illegal user-name chars:
    So, I don't know what you're doing, but white-listing is probably a better idea from what it looks like.

    If you need help with regular expressions, you should consider learning the basics first:

    The key difference from black-list and white-list.

    Black-List: Allow everything except those in the exception list.
    White-List: Block everything except those in the exception list.

    So, it's easy to tell which is more secure. If you block everything by default, you only need to narrow what should be allowed- which is easier to do- but still, like anything simple, can become complex.

    A concept (such as white-listing or black-listing) does not guarantee security. It only helps.
    Last edited by s-p-n; 03-01-2013 at 08:00 PM.

  3. #3
    Join Date
    Jul 2008
    urbana, il
    why not just do something like
     unescape( str ).replace( /['"]/g, "" )
    Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.10147 seconds
  • Memory Usage 2,944KB
  • Queries Executed 13 (?)
More Information
Template Usage (30):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (7)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (3)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (3)postbit
  • (3)postbit_onlinestatus
  • (3)postbit_wrapper
  • (1)showthread_list
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (27):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./includes/functions_threadedmode.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (71):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids_threaded
  • showthread_threaded_construct_link
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates