How to loop through a string for a regex match, concat return new full string
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
add a comment |
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
1
no need to loop, justreplace
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
add a comment |
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
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
javascript regex dictionary match
edited Nov 14 '18 at 19:16
benishky
asked Nov 13 '18 at 21:02
benishkybenishky
13312
13312
1
no need to loop, justreplace
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
add a comment |
1
no need to loop, justreplace
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
add a comment |
2 Answers
2
active
oldest
votes
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>
.
add a comment |
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);
1
The regular expression in the original attempt would capture multi-word items, but usingw
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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>
.
add a comment |
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>
.
add a comment |
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>
.
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)
edited Nov 13 '18 at 21:23
answered Nov 13 '18 at 21:14
HerohtarHerohtar
2,88411826
2,88411826
add a comment |
add a comment |
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);
1
The regular expression in the original attempt would capture multi-word items, but usingw
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
add a comment |
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);
1
The regular expression in the original attempt would capture multi-word items, but usingw
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
add a comment |
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);
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);
edited Nov 14 '18 at 2:30
answered Nov 13 '18 at 21:07
Dacre DennyDacre Denny
11.2k41030
11.2k41030
1
The regular expression in the original attempt would capture multi-word items, but usingw
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
add a comment |
1
The regular expression in the original attempt would capture multi-word items, but usingw
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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