Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

searching a string for and replace a regex experssion

  • 31-10-2003 9:41am
    #1
    Registered Users, Registered Users 2 Posts: 4,225 ✭✭✭


    Regex is doing my head in atm.
    heres a runction i've written in java script to do a character replacement on a string:

    function replaceChars(myString,myReplaceChars,myReplaceWith)
    {
    var functionName = "replaceChars"

    try {
    var replaceChars = myReplaceChars;

    var replaceWith = myReplaceWith;

    var replaceRegExp = new RegExp(replaceChars, 'gi');

    myString = String(myString);

    var retString = myString.replace(replaceRegExp, replaceWith);

    return retString;
    }
    catch(error) {
    throw( new Error(error.number, functionName + error.description) );
    }

    }

    I'm trying to search a string for regex elements "\s*" and replace that exact expression with a whitespace.

    for example if the string is and i call
    var newString = replaceChars("test\s*string" ,"\s*" ," ")
    the result is newString = "test\s*string"

    if i call var newString = replaceChars("test\s*string" ,"\\s\*" ," ") {trying to find the literal as opposed to the regex}
    the result is newString = "t e s t \ s * s t r i n g "

    any ideas?


Comments

  • Registered Users, Registered Users 2 Posts: 4,225 ✭✭✭Scruff


    ye can all turn yer thinking hats off, i eventually figured it myself :) (well trial and error)

    var newString = replaceChars("test\s*string" ,"\\\\s\\*" ," ")
    will return newString = "test string"

    marvelous. i know the \ character is the used to escape literalls in a regex expression though why i need all the \ characters i'm not 100% positive.can anyone explain that to me?


  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    Sure..although I'm guessing somewhat.

    You need to double-up the \ becase *java* will use it as an escape char.

    So, to have a string with just a \ in it, you would have :

    string myVar = "\\".

    Now, when you pass *that* to regexp, it will only see "\", which it will complain about...so your java string needs to pass double the quantity.

    So, you need to pass a string to regexp which is "\\", which - to create in your code - you will need to express as "\\\\".

    I'm just surprised you didn't have to double-up the first "\".

    jc


  • Closed Accounts Posts: 304 ✭✭Zaltais


    Yeah basically what bonkey said, but to phrase it slightly differently:
    Originally posted by Scruff
    i know the \ character is the used to escape literalls in a regex expression though why i need all the \ characters i'm not 100% positive

    Basically \ is a 'literal' as you've put it (or a metacharacter) in itself so you need to escape the literal (hence the need to use '\\\' to match '\\').

    In this instance it may be better to use the builtin JavaScript RegExp constructor function (as using this function I'm nearly sure you don't need to escape the '\\' metacharacters) ...

    This article is very good....


Advertisement