How to replace occurrences by key [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
This is a simple question about JavaScript,
Say I got the following string:
A)My Name B)My Name C)My Name
now I give to a function the key #1 expecting to replace the second occurrence of My Name inside that function and return:
A)My Name B) C)My Name
I haven't found the solution to this anywhere online so I'm asking.

You could use split to separate the string into the parts, then join the before and after back together:
function removeNthMatch(input, removeString, removeIndex) {
var splitString = input.split(removeString);
result = splitString.slice(0, removeIndex + 1).join(removeString)
+ splitString.slice(removeIndex + 1).join(removeString);
}
input = "A)My Name B)My Name C)My Name";
removeString = "My Name";
removeIndex = 1;
console.log(removeNthMatch(input, removeString, removeIndex));

Related

How to get the array of strings that is in between curly braces inside source string in JavaScript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Suppose the JavaScript variable is:
var sourceString="stack {overflow} is the best {place} to clear {technical} doubts";
Output javascript string array should contain: overflow,place,technical
or {overflow},{place},{technical}
I am fine with both the results.
You could use regex to accomplish this:
sourceString.match(/{.*?}/g)
var getMatchingGroups = function(s) {
var r=/\{(.*?)\}/g, a=[], m;
while (m = r.exec(s)) {
a.push(m[1]);
}
return a;
};
getMatchingGroups(sourceString) // ["overflow", "place", "technical"]

Converting a file to array with FS [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a simple "en.txt"
"TITLE" => "Amazing title of my page"
"COPYRIGHT" => "Copyright my site"
"BLABLA" => "A amazing sentence"
And I would like convert this in array for nodeJS with FS.
Thanks for your help.
First, read the file data using the fs.readFile method. Once you have the file data in a variable, you can convert it into an array using the following regular expression:
var regex = /"([^"]+)"\s*=>\s*"([^"]+)/g;
var match, results = {};
while((match = regex.exec(fileData)) !== null){
results[match[1]] = match[2];
}
console.log(results); // contains js array of data
See Fiddle.

Get text in getElementsByClassName [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am writing some javascript to be run in a bookmarklet that should get the text within a element that has a certain class name.
So for example
document.getElementsByClassName('price')
where the web page has or example
<span class="price">
£23
</span>
How would I go about getting the actual text within the the element that has a class name price (i.e £23 in the above example).
getElementsByClassName returns an array of elements. Traverse through that array and get the innerText property of each. For example:
var priceEls = document.getElementsByClassName("price");
for (var i = 0; i < priceEls.length; i++) {
var price = priceEls[i].innerText;
alert("Price: " + price);
}
Live demo: http://jsfiddle.net/YQsBW/

How can I convert this coffeescript to js/jquery? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
text = child.html()
text = text.replace /(#include(\s*<.*>)?)/gi, '<span>$1</span>'
text = text.replace /(main\(.*\))/gi, '<span>$1</span>'
child.html text
http://jsfiddle.net/dcro/XKHC8/
This is an answer from my question: Wrapping strings with a span I dont know how to use coffeescript and the one who answer looks unavailable.
From js2coffee.org
var text;
text = text.replace(/(#include(\s*<.*>)?)/gi, '<span>$1</span>');
text = text.replace(/(main\(.*\))/gi, '<span>$1</span>');
child.html(text);

Adding an id on to a href jquery [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to get the id of an radiobutton to be added to href when the link is clicked. I tried searching google for a long time. But maybe i'm missing because it's very basic.
I do have a bit of code written but it doesn't work. Help?
function ProductIDLink() {
var url = "/bbbb/aaaaaaa/" + ('.aaasaff:radio:checked').attr('id');
$("#safad").attr("href", url)
}
Looks like you've just forgotten a $:
... ('.aaasaff:radio:checked').attr('id')
should be
... $('.aaasaff:radio:checked').attr('id')
From what I can see the $ was missing.
function ProductIDLink() {
var url = "/bbbb/aaaaaaa/" + $('.aaasaff:radio:checked').attr('id');
$("#safad").attr("href", url)
}

Categories

Resources