Replace string that contains dynamically numbers [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
i have a variable that contains the string: attribute_value[XXX][] where XXX every time that page loads is a 3-digit number that changes every time.
How can i use .replace function in javascript to replace attribute_value[XXX][] with something specific regardless the XXX value?
Thank you in advance

You can use regular expression for that
str.replace(/attribute_value\d+/, 'aa');
here '\d' indicates digits and '+' indicates one or more occurances
str.replace(/attribute_value\d{3}/, 'aa');
Use this if you want to replace only first 3 digits

Related

RegEx: Matching HTML tags and text in a string of HTML [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
This post was edited and submitted for review 7 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have written a regular expression which matches all header HTML elements, including their text. The present regular expression I am using is: /<(h[1-6])[^>]*>(.*?)<\/\1>/gi.
I want to match all headings and text until testing-options appears in the string below:
<h1>jfhfnjkgf</h1><h2>fegsg</h2><h2>fwegrweghrw</h2><h4>sfadfsaf</h4><h4>ukfdsnsjkfsd</h4><ac:structured-macro ac:name="testing-options" ac:schema-version="1" data-layout="default" ac:local-id="8bbfe293-eeb9-4785-9873-9fb9b218692b" ac:macro-id="bed737bbfaaf5f1df5f68a0ecc0bc6a7"><ac:parameter ac:name="enable-testing">disable</ac:parameter></ac:structured-macro><h3>bkbkvwv</h3><h4>vbdkhvbkwv</h4><h3>v n vw bv</h3><p />
The final regex provided should match every heading and text until testing options appears in the string above. In this case, the match should be: <h1>jfhfnjkgf</h1><h2>fegsg</h2><h2>fwegrweghrw</h2><h4>sfadfsaf</h4><h4>ukfdsnsjkfsd</h4>.
Use the following Regex without global flag
(<h[1-6]>\w+<\/h[1-6]>)*

extracting letters from an array of strings [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have an array of strings denoting variants of shirts in my store.
Here is an example string:
Apparel-DTG-Tshirt-District-DT6000-M-Black-Mens-CF-20200304113232677
All of the strings share same structure and the rule that I need to implement is, extract what's in between 5th and 6th occurrence of - (dash). In this particular case this is M.
How do I do that?
This would help you. The first capturing group will be the value between fifth and sixth dash:
https://regex101.com/r/UlQBqy/2
const re = /^(?:[^-]*-){5}(.*?)(?=-)/
console.log("Apparel-DTG-Tshirt-District-DT6000-M-Black-Mens-CF-20200304113232677".match(re)[1])

Regular expression (Regex) to allow only number or specific character with number at a time [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a requirement to allow any digits with only one character (either w or d or W or D) at a time with no sequence.
For example: 2 or 2w or 222W or 2d or 222D
(not like 2wd, 55dw, 2w5d, 2w5w, 2D5D etc)
Could you please provide solution on this. Thanks.
Try the following:
^\d+[wdWD]?$
Try it out:https://regex101.com/r/sYqjTi/1
If you don't want it to match just 2, remove the ?, it will make the last letter required.
This regex should work both on Java and JavaScript.

Regex with substitution around a number [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a problem in some substitution in javascript (HTML).
I need to substitute ONLY the tags around every number, e.g.
<1>
become:
<sub>1</sub>
or
<20>
become
<sub>20</sub>
etc.
How can I do?
Thank you so much!
You can use replace and regex with groups to achieve this:
var text = '<1>blabla<20>';
var newText = text.replace(/<(\d+)>/g, '<sub>$1</sub>');
console.log(newText);

JavaScript RegEx Replace all but one word contains substring [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I need to replace all word in except one word. the first word that contains "Thewo"
How can i do this with a RegEx?
Input String "We need to remove something from and keep a word Theword and save only first word Thewood Theworld"
Expted exsult: Theword
This will do what you want:
input.match(/\w*Thewo\w*/)
Since you are asking for the first word that contains "Thewo", I assume that "Thewo" can appear in the middle of a word. For example, it will match notTheworker in "and notTheworker here".

Categories

Resources