jquery / JavaScript replace xth occurance

I have a string like this :

var str = "A A A A A";

how do I replace a specific A with something else ?

Eg: replace 3rd A to :

var str = "A A 00 A A";

Of the 1st, 2nd, etc.. ?


-------------Problems Reply------------

I would split the str, replace whatever index, then join back

var str = "A A A A A";
var sp=str.split(' ');
var ind=2;
console.log(sp.join(' '));

I prefer to use regular expression, here is the answer:

newValue = str.replace(/(([^A]*A){2}[^A]*)A/g, '$100');

Notes: {2} is the number of the occurrence we want to replace.

$100 here we specify which is the new string we want to place after the $1

Category:javascript Views:1 Time:2019-03-01

Related post

  • jQuery / Javascript replace function only works once 2011-08-26

    I have a button as such <button id="toggle_photo"> <span id="photo_text">Hide Photos (1)</span> <img class="icon" alt="Photos" src="/img/photos-16.png"> </button> And jQuery / Javascript to change the text upon clicking

  • jQuery/javascript replace tag type 2010-05-12

    Is there an easy way to loop through all td tags and change them to th? (etc). My current approach would be to wrap them with the th and then remove the td, but then I lose other properties etc. --------------Solutions------------- Completely unteste

  • How do I use JQuery to replace all occurring of a certain word in a webpage? 2011-03-01

    I've made two different attempts to replace all occurrences of a single word from a webpage using JQuery in a Chrome extension. Both attempts kind of worked, but neither worked as a universal method for replacing all occurrences of a word from a webp

  • jquery/javascript replace image filename in path 2011-10-25

    i need to replace a filename in a path. I found a few solutions for this but I couldnt get any of them to work with my particular situation. I have 3 elements to work with here.... the trigger image: http://domainname.com/path/to/files/product_thumb_

  • jquery/javascript: replace string inside of html()? 2011-03-07

    the following variable … var breadcrumbs = $('#trail').html(); … contains: <href="page1/">Page 1</a> <strong>»</strong> <a href="page2">Page 2</a> <strong>»</strong> <a href="page3">Page 3</a

  • Jquery Javascript Replace String 2011-08-06

    The wysiwyg editor I'm using uses this to replace the <strong> with a span style. I modified to add a code block. v=v.replace(/<strong>(.*)<\/strong>/gi,'<span style="font-weight: bold;">$1</span>'); This works fine unti

  • How to Use Jquery Javascript Replace 2011-09-20

    I'm using this in my wysiwyg to replace < pre> Hi my name is< /pre> with < div id="precode">Hi my name is< /div> This is the working code code v=v.replace(/<pre>(.*)<\/pre>/gim,'<div id="precode">$1</div

  • Replace comma seperation with new line (jQuery JavaScript Library) 2010-12-12

    I am using the following example code here: http://jsbin.com/ujuse It uses the jQuery JavaScript Library. How can I change it so that when you select a tick box, the comma is replaced with a new line instead? I need the text area to display: one_name

  • JavaScript/jQuery Regex Replace Input Field with Valid Characters 2011-07-03

    I am building a CMS where a user can customize the SEO URL of a page via a text input control. For example, say the user is creating a gallery, and they want their page to be accessed at http://www.theirsite.com/my-1st-gallery. Notice how the "my-1st

  • Performance of Javascript replace vs jQuery replace 2012-04-10

    In another question I came across two different ways of doing a string replacement. One being the jQuery way $("#element").text().replace(',', '.') and the other being the pure Javascript way of first getting the text and then calling .replace(/,/, '

  • Replacing dynamic content jQuery/javascript 2013-06-14

    I need to do a dynamic replace in a string with jQuery. I have read about using wildcards in selectors, but I want it in a string. Considering this HTML: <style type="text/css">@import url('css/another.css');</style> <style type="text/

  • Javascript replace - not working with string of html 2011-12-21

    I'm trying to create a sort of 'template' in plain old html, which can then have elements inserted by another javascript function. So, I've got var string = "<div id='<@nameInsert>' style='padding: 2px;'>here's some text and stuff</div

  • Cronjob but for jQuery/Javascript 2011-01-14

    I'm trying to develop a web application that mainly uses PHP but i'm using jQuery/Javascript to grab people's Tweets from their URL: http://twitter.com/status/user_timeline/joebloggs.json?count=1&callback=? The thing is want to run a PHP cron job

  • Move table rows up and down - Jquery/Javascript 2011-02-10

    Can anyone please tell me how to move table rows up and down through jquery/javascript. I have a table and for each row a radio button is there in the first td. Onclicking the up/down arrows the selected rows should move up/down. Looking forward for

  • Jquery / Javascript speed- should I really care? 2011-03-02

    I've seen lots of posts comparing the speeds of various selector queries and DOM traversal methods. Of course it matters in cases with hundreds or thousands of elements and O^n operations, but does speed really matter in the 99% of cases where Jquery

  • how can I cut a string that contain "map" in jquery/javascript 2011-04-28

    Does anyone know how can I cut a string that contain "map" in jquery/javascript Example: If i have a string var stringName = "abc mapTest bbc"; How Can I cut the string by result only mapTest? NOTE: like assume the stringName is not the fixed value,

  • How to secure my webservices through JQuery/Javascript calls? 2011-06-23

    Suppose I have created a web service (DataOperation.asmx) within my web application. Now I am calling this webservice through my .aspx page using JQuery. like: $.ajax({ type: "POST", url: "../../DataOperation.asmx/someMethod", cache: false, data: "pa

  • Custom Animations with Jquery/Javascript 2011-06-27

    Forgive me if this is not the best forum to ask this question in. I'm not sure where to start, so maybe someone here can direct me. Using html and jquery/javascript, I want an animation of one item flipping over another item. Almost as if someone is

  • Clicking 'OK' on alert or confirm dialog through jquery/javascript? 2011-10-09

    I was thinking of writing some UI tests in backbone.js and jquery. They may not be the best way to do it but it's something that I was thinking about - to automate the tests without record and playback - through plain code. The only thing that made m

  • programatically trigger to .change event with jquery/javascript? 2011-11-10

    Is there a way to programatically trigger to .change event with jquery/javascript? I want a piece of code that will fire a .change event for a select/option box such as the following... <div data-role='fieldcontain' class='none'> <select nam

  • javascript replace all with a javascript expression? 2011-12-02

    I know that this regexp replace: something.replace(/something/g,"something"); replaces every occurrence in a string, but how do I replace all of something with a jQuery/JavaScript expression because something like this : newhtml=newhtml.replace(wrap(

  • javascript replace() regexp returning syntax error 2012-02-13

    I'm trying out YQL with Jquery, and some javascript replace regexp returns a syntax error: data = data.replace(/<?/body[^>]*>/g,''); data = data.replace(/<noscript[^>]*>[Ss]*?</noscript>/g,''); data = data.replace(/<script[

  • jquery/javascript change color of text depending on day 2012-02-24

    I am trying to make an 'opening hours' table that highlights the current day for the user. HTML: <div id="Monday"> <h2> Mon</h2><h3>8am-9pm</h3></div> <div id="Tuesday"> <h2> Tue</h2><h3>8am

  • Jquery to replace SRC for img tag 2012-03-03

    <img src="linkclick.aspx?fileticket=0" id="directory-image"> Let's say when this comes up, I want to replace "linkclick.aspx?fileticket=0" with "images/no-avatar.gif" I assume this can be done with Jquery, but I can't easily figure it out. <

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.098 (s). 11 q(s)