替换字符串中的数字

时间:2016-04-29 16:18:59

标签: javascript jquery regex

我目前有这段代码。

[ERROR] Exception in thread "pool-1-thread-96" java.lang.NullPointerException
[ERROR]     at com.google.gwt.dev.util.DiskCache.transferToStream(DiskCache.java:185)
[ERROR]     at com.google.gwt.dev.util.DiskCacheToken.writeObject(DiskCacheToken.java:73)
[ERROR]     at sun.reflect.GeneratedMethodAccessor29.invoke(Unknown Source)
[ERROR]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[ERROR]     at java.lang.reflect.Method.invoke(Method.java:498)
[ERROR]     at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:1028)
[ERROR]     at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1496)
[ERROR]     at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
[ERROR]     at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
[ERROR]     at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
[ERROR]     at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:441)
[ERROR]     at com.google.gwt.dev.javac.CachedCompilationUnit.writeObject(CachedCompilationUnit.java:222)
[ERROR]     at sun.reflect.GeneratedMethodAccessor33.invoke(Unknown Source)
[ERROR]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[ERROR]     at java.lang.reflect.Method.invoke(Method.java:498)
[ERROR]     at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:1028)
[ERROR]     at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1496)
[ERROR]     at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
[ERROR]     at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
[ERROR]     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
[ERROR]     at com.google.gwt.dev.javac.PersistentUnitCache$6.run(PersistentUnitCache.java:492)
[ERROR]     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
[ERROR]     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
[ERROR]     at java.lang.Thread.run(Thread.java:745)

我想要做的就是替换380'和' 583'在src字符串中有860和1318.我在这里读了几个问题,但他们实际上并没有找到并用其他数字替换它们。

任何帮助都会很棒!

2 个答案:

答案 0 :(得分:1)

您可以使用replace()

var url = $(this).attr("src").replace("380","860").replace("583","1318");

完成功能:

var result = {
    productName: $(".product-details .product-title").text().trim(),
    description: $(".product-details .product-description p").text().trim(),
    price: $(".product-details .price-lockup meta[itemprop='lowPrice']").attr("content"),
    imageUrls: []
};

$('.image-cache img[src*="?fit=fill&bg=fff&fm=jpg&q=60&w=380&h=583"]').each( function() {
    var url = $(this).attr("src").replace("380","860").replace("583","1318");
    result.imageUrls.push(url);
});

答案 1 :(得分:0)

只需使用字符串replace()方法。

url = url.replace('380', '860');
url = url.replace('583', '1318');