How to loop through a string for a regex match, concat return new full string












1















I'm trying to achieve:



const finalStr = "team='Core', team='Mechanics'"
//loop through string, get single quotes, add <bold>'Core'</bold>
//I want to return the string:
"team=<bold>'Core'</bold>, team=<bold>'Mechanics'</bold>"


What I've tried, but obviously wrong...can't wrap my head around it:



const finalStr = this.state.finalString
const newFinal = finalStr.match(/'(.*?)'/g).map(item => {
item = item.replace(item, '<b>' + item + '</b>')
return item;
});









share|improve this question




















  • 1





    no need to loop, just replace once: replace(/.../g, '<b>$&</b>')

    – georg
    Nov 13 '18 at 21:05













  • Are you trying to generate HTML with that? If so, the correct tag is <b>, not <bold>, as that tag doesn't exist.

    – Herohtar
    Nov 13 '18 at 21:16











  • thanks @georg Perfect! I over-complicated it as usual.

    – benishky
    Nov 13 '18 at 21:19
















1















I'm trying to achieve:



const finalStr = "team='Core', team='Mechanics'"
//loop through string, get single quotes, add <bold>'Core'</bold>
//I want to return the string:
"team=<bold>'Core'</bold>, team=<bold>'Mechanics'</bold>"


What I've tried, but obviously wrong...can't wrap my head around it:



const finalStr = this.state.finalString
const newFinal = finalStr.match(/'(.*?)'/g).map(item => {
item = item.replace(item, '<b>' + item + '</b>')
return item;
});









share|improve this question




















  • 1





    no need to loop, just replace once: replace(/.../g, '<b>$&</b>')

    – georg
    Nov 13 '18 at 21:05













  • Are you trying to generate HTML with that? If so, the correct tag is <b>, not <bold>, as that tag doesn't exist.

    – Herohtar
    Nov 13 '18 at 21:16











  • thanks @georg Perfect! I over-complicated it as usual.

    – benishky
    Nov 13 '18 at 21:19














1












1








1








I'm trying to achieve:



const finalStr = "team='Core', team='Mechanics'"
//loop through string, get single quotes, add <bold>'Core'</bold>
//I want to return the string:
"team=<bold>'Core'</bold>, team=<bold>'Mechanics'</bold>"


What I've tried, but obviously wrong...can't wrap my head around it:



const finalStr = this.state.finalString
const newFinal = finalStr.match(/'(.*?)'/g).map(item => {
item = item.replace(item, '<b>' + item + '</b>')
return item;
});









share|improve this question
















I'm trying to achieve:



const finalStr = "team='Core', team='Mechanics'"
//loop through string, get single quotes, add <bold>'Core'</bold>
//I want to return the string:
"team=<bold>'Core'</bold>, team=<bold>'Mechanics'</bold>"


What I've tried, but obviously wrong...can't wrap my head around it:



const finalStr = this.state.finalString
const newFinal = finalStr.match(/'(.*?)'/g).map(item => {
item = item.replace(item, '<b>' + item + '</b>')
return item;
});






javascript regex dictionary match






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 19:16







benishky

















asked Nov 13 '18 at 21:02









benishkybenishky

13312




13312








  • 1





    no need to loop, just replace once: replace(/.../g, '<b>$&</b>')

    – georg
    Nov 13 '18 at 21:05













  • Are you trying to generate HTML with that? If so, the correct tag is <b>, not <bold>, as that tag doesn't exist.

    – Herohtar
    Nov 13 '18 at 21:16











  • thanks @georg Perfect! I over-complicated it as usual.

    – benishky
    Nov 13 '18 at 21:19














  • 1





    no need to loop, just replace once: replace(/.../g, '<b>$&</b>')

    – georg
    Nov 13 '18 at 21:05













  • Are you trying to generate HTML with that? If so, the correct tag is <b>, not <bold>, as that tag doesn't exist.

    – Herohtar
    Nov 13 '18 at 21:16











  • thanks @georg Perfect! I over-complicated it as usual.

    – benishky
    Nov 13 '18 at 21:19








1




1





no need to loop, just replace once: replace(/.../g, '<b>$&</b>')

– georg
Nov 13 '18 at 21:05







no need to loop, just replace once: replace(/.../g, '<b>$&</b>')

– georg
Nov 13 '18 at 21:05















Are you trying to generate HTML with that? If so, the correct tag is <b>, not <bold>, as that tag doesn't exist.

– Herohtar
Nov 13 '18 at 21:16





Are you trying to generate HTML with that? If so, the correct tag is <b>, not <bold>, as that tag doesn't exist.

– Herohtar
Nov 13 '18 at 21:16













thanks @georg Perfect! I over-complicated it as usual.

– benishky
Nov 13 '18 at 21:19





thanks @georg Perfect! I over-complicated it as usual.

– benishky
Nov 13 '18 at 21:19












2 Answers
2






active

oldest

votes


















3














You don't need a callback or any additional functions, just use the replacement pattern described in the String.replace() documentation to insert the matched substring ($&). You also don't need the parenthesis for the capture group unless you're intending to do something else with the matches.






const finalStr = "team='Core', team='Mechanics'"

const newFinal = finalStr.replace(/'.*?'/g, '<bold>$&</bold>')
console.log(newFinal)





As a side note, there is no <bold> tag in HTML, so if you are trying to create valid HTML you should be using <b>.






share|improve this answer

































    1














    You could use the same basic regular expression, /'.*?'/gi, with a custom "replacer" callback passed to the string#replace method to solve this:






    const input = "team='Core', team='Mechanics'"

    const output = input.replace(/'.*?'/gi, function(matchStr) {

    // Wrap each match in the resulting string with <bold /> tags
    return '<bold>' + matchStr + '</bold>';
    });

    console.log(output);








    share|improve this answer





















    • 1





      The regular expression in the original attempt would capture multi-word items, but using w prevents this, so it won't produce exactly the same results. OP didn't specify that as a requirement, but it might be good to note.

      – Herohtar
      Nov 13 '18 at 21:26











    • @Herohtar thanks for the feedback - very good point, I'd only paid attention to the input data in the OP and hadn't considered that possibility of white-spaces etc. Thanks again!

      – Dacre Denny
      Nov 14 '18 at 2:31











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289446%2fhow-to-loop-through-a-string-for-a-regex-match-concat-return-new-full-string%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    You don't need a callback or any additional functions, just use the replacement pattern described in the String.replace() documentation to insert the matched substring ($&). You also don't need the parenthesis for the capture group unless you're intending to do something else with the matches.






    const finalStr = "team='Core', team='Mechanics'"

    const newFinal = finalStr.replace(/'.*?'/g, '<bold>$&</bold>')
    console.log(newFinal)





    As a side note, there is no <bold> tag in HTML, so if you are trying to create valid HTML you should be using <b>.






    share|improve this answer






























      3














      You don't need a callback or any additional functions, just use the replacement pattern described in the String.replace() documentation to insert the matched substring ($&). You also don't need the parenthesis for the capture group unless you're intending to do something else with the matches.






      const finalStr = "team='Core', team='Mechanics'"

      const newFinal = finalStr.replace(/'.*?'/g, '<bold>$&</bold>')
      console.log(newFinal)





      As a side note, there is no <bold> tag in HTML, so if you are trying to create valid HTML you should be using <b>.






      share|improve this answer




























        3












        3








        3







        You don't need a callback or any additional functions, just use the replacement pattern described in the String.replace() documentation to insert the matched substring ($&). You also don't need the parenthesis for the capture group unless you're intending to do something else with the matches.






        const finalStr = "team='Core', team='Mechanics'"

        const newFinal = finalStr.replace(/'.*?'/g, '<bold>$&</bold>')
        console.log(newFinal)





        As a side note, there is no <bold> tag in HTML, so if you are trying to create valid HTML you should be using <b>.






        share|improve this answer















        You don't need a callback or any additional functions, just use the replacement pattern described in the String.replace() documentation to insert the matched substring ($&). You also don't need the parenthesis for the capture group unless you're intending to do something else with the matches.






        const finalStr = "team='Core', team='Mechanics'"

        const newFinal = finalStr.replace(/'.*?'/g, '<bold>$&</bold>')
        console.log(newFinal)





        As a side note, there is no <bold> tag in HTML, so if you are trying to create valid HTML you should be using <b>.






        const finalStr = "team='Core', team='Mechanics'"

        const newFinal = finalStr.replace(/'.*?'/g, '<bold>$&</bold>')
        console.log(newFinal)





        const finalStr = "team='Core', team='Mechanics'"

        const newFinal = finalStr.replace(/'.*?'/g, '<bold>$&</bold>')
        console.log(newFinal)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 21:23

























        answered Nov 13 '18 at 21:14









        HerohtarHerohtar

        2,89111826




        2,89111826

























            1














            You could use the same basic regular expression, /'.*?'/gi, with a custom "replacer" callback passed to the string#replace method to solve this:






            const input = "team='Core', team='Mechanics'"

            const output = input.replace(/'.*?'/gi, function(matchStr) {

            // Wrap each match in the resulting string with <bold /> tags
            return '<bold>' + matchStr + '</bold>';
            });

            console.log(output);








            share|improve this answer





















            • 1





              The regular expression in the original attempt would capture multi-word items, but using w prevents this, so it won't produce exactly the same results. OP didn't specify that as a requirement, but it might be good to note.

              – Herohtar
              Nov 13 '18 at 21:26











            • @Herohtar thanks for the feedback - very good point, I'd only paid attention to the input data in the OP and hadn't considered that possibility of white-spaces etc. Thanks again!

              – Dacre Denny
              Nov 14 '18 at 2:31
















            1














            You could use the same basic regular expression, /'.*?'/gi, with a custom "replacer" callback passed to the string#replace method to solve this:






            const input = "team='Core', team='Mechanics'"

            const output = input.replace(/'.*?'/gi, function(matchStr) {

            // Wrap each match in the resulting string with <bold /> tags
            return '<bold>' + matchStr + '</bold>';
            });

            console.log(output);








            share|improve this answer





















            • 1





              The regular expression in the original attempt would capture multi-word items, but using w prevents this, so it won't produce exactly the same results. OP didn't specify that as a requirement, but it might be good to note.

              – Herohtar
              Nov 13 '18 at 21:26











            • @Herohtar thanks for the feedback - very good point, I'd only paid attention to the input data in the OP and hadn't considered that possibility of white-spaces etc. Thanks again!

              – Dacre Denny
              Nov 14 '18 at 2:31














            1












            1








            1







            You could use the same basic regular expression, /'.*?'/gi, with a custom "replacer" callback passed to the string#replace method to solve this:






            const input = "team='Core', team='Mechanics'"

            const output = input.replace(/'.*?'/gi, function(matchStr) {

            // Wrap each match in the resulting string with <bold /> tags
            return '<bold>' + matchStr + '</bold>';
            });

            console.log(output);








            share|improve this answer















            You could use the same basic regular expression, /'.*?'/gi, with a custom "replacer" callback passed to the string#replace method to solve this:






            const input = "team='Core', team='Mechanics'"

            const output = input.replace(/'.*?'/gi, function(matchStr) {

            // Wrap each match in the resulting string with <bold /> tags
            return '<bold>' + matchStr + '</bold>';
            });

            console.log(output);








            const input = "team='Core', team='Mechanics'"

            const output = input.replace(/'.*?'/gi, function(matchStr) {

            // Wrap each match in the resulting string with <bold /> tags
            return '<bold>' + matchStr + '</bold>';
            });

            console.log(output);





            const input = "team='Core', team='Mechanics'"

            const output = input.replace(/'.*?'/gi, function(matchStr) {

            // Wrap each match in the resulting string with <bold /> tags
            return '<bold>' + matchStr + '</bold>';
            });

            console.log(output);






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 14 '18 at 2:30

























            answered Nov 13 '18 at 21:07









            Dacre DennyDacre Denny

            11.6k41031




            11.6k41031








            • 1





              The regular expression in the original attempt would capture multi-word items, but using w prevents this, so it won't produce exactly the same results. OP didn't specify that as a requirement, but it might be good to note.

              – Herohtar
              Nov 13 '18 at 21:26











            • @Herohtar thanks for the feedback - very good point, I'd only paid attention to the input data in the OP and hadn't considered that possibility of white-spaces etc. Thanks again!

              – Dacre Denny
              Nov 14 '18 at 2:31














            • 1





              The regular expression in the original attempt would capture multi-word items, but using w prevents this, so it won't produce exactly the same results. OP didn't specify that as a requirement, but it might be good to note.

              – Herohtar
              Nov 13 '18 at 21:26











            • @Herohtar thanks for the feedback - very good point, I'd only paid attention to the input data in the OP and hadn't considered that possibility of white-spaces etc. Thanks again!

              – Dacre Denny
              Nov 14 '18 at 2:31








            1




            1





            The regular expression in the original attempt would capture multi-word items, but using w prevents this, so it won't produce exactly the same results. OP didn't specify that as a requirement, but it might be good to note.

            – Herohtar
            Nov 13 '18 at 21:26





            The regular expression in the original attempt would capture multi-word items, but using w prevents this, so it won't produce exactly the same results. OP didn't specify that as a requirement, but it might be good to note.

            – Herohtar
            Nov 13 '18 at 21:26













            @Herohtar thanks for the feedback - very good point, I'd only paid attention to the input data in the OP and hadn't considered that possibility of white-spaces etc. Thanks again!

            – Dacre Denny
            Nov 14 '18 at 2:31





            @Herohtar thanks for the feedback - very good point, I'd only paid attention to the input data in the OP and hadn't considered that possibility of white-spaces etc. Thanks again!

            – Dacre Denny
            Nov 14 '18 at 2:31


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289446%2fhow-to-loop-through-a-string-for-a-regex-match-concat-return-new-full-string%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues