English 中文(简体)
替换 Java文中部分插图
原标题:Replace part of a string in JavaScript

How can I replace:

var url = "http://localhost:2879/ServiceDonneesArchive.svc/Installations(1002)?$expand=Stations";

:

var nameInstallation = 1002;
    var url = "http://localhost:2879/ServiceDonneesArchive.svc/Installations(nameInstallation)?$expand=Stations";
问题回答

Why do this the hard way? For this use case, simple concatenation would be very readable:

var nameInstallation = 1002;
var url =  http://localhost:2879/ServiceDonneesArchive.svc/Installations(  + nameInstallation +  )?$expand=Stations ;

使用https://developer.mozilla.org/en/Java/Reference/Global_Objects/String/replace” rel=“nofollow”>.replace(meth。 以<代码>1002取代<编码> 姓名/编码>在<代码>url下改动的<代码>1002>:

url = url.replace(/nameInstallation/g, "1002");

或者,如果您在可变的<代码>中具有替换价值,即名称:Installation = 1002:

url = url.replace(/nameInstallation/g, nameInstallation);

EDIT: As pointed out by David Thomas, you probably don t need the g flag on the regular expression that is the first parameter to .replace(). With this "global" flag it will replace all instances of the text "nameInstallation". Without the flag it would replace only the first instance. So either include it or leave it off according to your needs. (If you only need to replace the first occurrence you also have the option of passing a string as the first parameter rather than a regex.)

履行这一规定的职能

// from http://www.codeproject.com/Tips/201899/String-Format-in-JavaScript
        String.prototype.format = function (args) {
            var str = this;
            return str.replace(String.prototype.format.regex, function(item) {
                var intVal = parseInt(item.substring(1, item.length - 1));
                var replace;
                if (intVal >= 0) {
                    replace = args[intVal];
                } else if (intVal === -1) {
                    replace = "{";
                } else if (intVal === -2) {
                    replace = "}";
                } else {
                    replace = "";
                }
                return replace;
            });
        };
        String.prototype.format.regex = new RegExp("{-?[0-9]+}", "g");

使用:

var url = "http://localhost:2879/ServiceDonneesArchive.svc/Installations{0}?$expand=Stations";
var nameInstallation = 1002;
var result = url.format(nameInstallation );




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签