Multiple Regex on String

Posted by George on Stack Overflow See other posts from Stack Overflow or by George
Published on 2010-05-10T16:04:39Z Indexed on 2010/05/10 16:24 UTC
Read the original article Hit count: 183

Filed under:
|
|

How can I apply multiple regexs to a single string?

For instance, a user inputs the following into a text area:

red bird
blue cat
black dog

and I want to replace each carriage return with a comma and each space with an underscore so the final string reads as red_bird,blue_cat,black_dog.

I've tried variations in syntax along the lines of the following so far:

function formatTextArea() {
    var textString = document.getElementById('userinput').value;

    var formatText = textString.replace(
        new RegExp( "\\n", "g" ),",",
        new RegExp( "\\s", "g"),"_");

    alert(formatText);
}

© Stack Overflow or respective owner

Related posts about regex

Related posts about JavaScript