最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Multiple Regex on String - Stack Overflow

programmeradmin5浏览0评论

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 ma 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);
}

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 ma 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);
}
Share Improve this question asked May 10, 2010 at 16:04 ChoyChoy 2,11711 gold badges39 silver badges49 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 7

You can chain the replacements. Every application of the replace method retuns a string, so on that string you can apply replace again. Like:

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

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

    alert(formatText);
}

There's no need for all these new Regular Expression Objects by the way. Use Regular Expression literals (like /\n/g in the above).

Alternatively you can use a lambda for the replacement

const str = `red bird
blue cat
black dog`;
console.log(str.replace(/[\n\s]/g, a => /\n/.test(a) ? "," : "_"));

formatText = textString.replace(/\n/g,',').replace(/\s/g,'_');

As others have mentioned, chaining is good enough for something as simple as what you're asking. However, if you want this to be more dynamic, you can use a replacer function as the second argument:

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

    var formatText = textString.replace(/\n|\s/g, function ($0) {
        if ($0 === "\n")
            return ",";
        else if ($0 === " ")
            return "_";
    }
    alert(formatText);
}

Using a replacer function will allow you to be dynamic without having to chain together calls to replace(). It may also be marginally faster (regex parser is invoked only once). Be aware that \s will match more than just the space character, though :-) For the purposes of your question, this would be good enough:

var formatText = textString.replace(/\n|\s/g, function ($0) {
    return $0 == "\n" ? "," : "_";
}

Regexp object have their own literal notation, using forward slashes, meaning that backslashes don't have to be escaped. For example, /\n/g is equivalent to new RegExp('\\n','g').

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

    textString = textString.replace(/\n/g,",").replace(/\s/g,"_");

    alert(textString);
}
var textString = "red blue\nhack bye\ntest rt\n";
var formatText = textString.replace(new RegExp( "\\n", "g" ),",").replace(new RegExp( "\\s", "g"),"_");
alert(formatText);

Include http://phpjs/functions/str_replace:527 and then

input = str_replace("\\n", ',', input);
input = str_replace(' ', '_', input); 
发布评论

评论列表(0)

  1. 暂无评论