JavaScript String replace with regex by using captured match as key string replace with regex,string replace with regex python,string

Let's say I have this string:

<i id="1"></i><i id="2"></i><i id="3"></i>

and this object:

{
  "1": "Red",
  "2": "Green",
  "3": "Blue"
}

Then I want to obtain this string:

<i color="Red"></i><i color="Green"></i><i color="Blue"></i>

Would be possible to make it through a regex replace?

I've tried this:

var stringIwant = stringIhave.replace(/id="(\d)"/g, 'color="' + myObject["$1"] + '"')

But it tries to read the property "$1" of the object, which doesn't exist. I also tried removing the quotes around $1:

var stringIwant = stringIhave.replace(/id="(\d)"/g, 'color="' + myObject[$1] + '"')

But I obtain a ReferenceError: $1 is not defined

I've tried more things, but nothing worth mentioning.

That's why I wonder if this is even possible. Any help?

Answer:1

You can do that with callbacks:

const stringIhave = `<i id="1"></i><i id="2"></i><i id="3"></i>`;

const obj = {
  "1": "Red",
  "2": "Green",
  "3": "Blue"
};

const stringIwant = stringIhave.replace(/id="(\d)"/g, (mat, grp) => `color="${obj[grp]}"`
);

console.log(stringIwant);
Answer:2

I am trying to toggle plus and minus span and also toggle the answer div all at once and its not working out. I built the html so the question div has two childrens: div icons with two children of ...

I am trying to toggle plus and minus span and also toggle the answer div all at once and its not working out. I built the html so the question div has two childrens: div icons with two children of ...

  1. jquery toggle class onclick
  2. jquery toggle class animation
  3. jquery toggleclass not working
  4. jquery toggle class on hover
  5. jquery toggle class multiple
  6. jquery toggle class based on boolean
  7. jquery toggle class on button click
  8. jquery toggleclass delay
  9. jquery toggle class hide show
  10. jquery toggle class codepen
  11. jquery toggle class on scroll
  12. jquery toggle class condition
  13. jquery toggle class function
  14. jquery toggle class click
  15. jquery toggle class callback
  16. jquery toggle class by id
  17. jquery toggleclass off
  18. jquery toggle class visibility
  19. jquery toggle class this
  20. jquery toggle class on focus

I am a beginner with typescript and playing with it on their Playground site. I have the following code: class Queue<T> { private data = []; push = (item: T) => this.data.push(item)...

I am a beginner with typescript and playing with it on their Playground site. I have the following code: class Queue<T> { private data = []; push = (item: T) => this.data.push(item)...

  1. typescript doesn't compile
  2. typescript doesn't compile on save
  3. typescript doesn't work
  4. typescript doesn't understand
  5. typescript does not like
  6. typescript import doesn't work
  7. typescript instanceof doesn't work
  8. typescript exclude doesn't work
  9. typescript concat doesn't work
  10. typescript await doesn't work
  11. typescript filter doesn't work
  12. typescript await doesn't wait
  13. typescript foreach doesn't work
  14. typescript getter doesn't work
  15. typescript regex doesn't work
  16. typescript settimeout doesn't work
  17. typescript object doesn't support this action
  18. typescript includes does not work
  19. typescript typings doesn't work
  20. typescript property does not exists

I've been struggling with sending a POST request and fetching data back using Node.js I've been trialing like this var querystring = require('querystring'); var http = require('http'); var postData ...

I've been struggling with sending a POST request and fetching data back using Node.js I've been trialing like this var querystring = require('querystring'); var http = require('http'); var postData ...

  1. send post request javascript
  2. send post request python
  3. send post request curl
  4. send post request from chrome
  5. send post request online
  6. send post request java
  7. send post request postman
  8. send post request c#
  9. send post request powershell
  10. send post request php
  11. send post request from command line
  12. send post request golang
  13. send post request react
  14. send post request js
  15. send post request from terminal
  16. send post request html
  17. send post request in url
  18. send post request node
  19. send post request with parameters
  20. send post request to localhost

I am attempting to use the canActivate feature on my app routing, however whenever I compile the app the log shows constantly refreshing with fail messages that I had console.log()'d out for ...

I am attempting to use the canActivate feature on my app routing, however whenever I compile the app the log shows constantly refreshing with fail messages that I had console.log()'d out for ...