字符串替换表达式

时间:2017-07-07 10:31:02

标签: javascript regex string replace

我有以下字符串:

/bin

我想在每次添加新名称时替换整行。上面的内容会将名称附加到字符串的末尾。

每次调用 /Just implement one line/ .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND) /******************************/ notification = mBuilder.setSmallIcon(R.mipmap.ic_launcher) .setTicker(title) .setShowWhen(true) .setAutoCancel(true) .setContentTitle(title) .setContentIntent(resultPendingIntent) .setSound(alarmSound) .setStyle(inboxStyle) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND).setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher)) .setContentText(message) .build(); /**Implement below Method***/ /**And call for "wakeUpLock" Method inside onMessageReceived()**/ private void wakeUpLock() { PowerManager pm = (PowerManager)MyGcmListenerService.this.getSystemService(Context.POWER_SERVICE); boolean isScreenOn = pm.isScreenOn(); Log.i(TAG, "screen on: "+ isScreenOn); if(isScreenOn==false) { Log.i(TAG, "screen on if: "+ isScreenOn); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE,"MyLock"); wl.acquire(10000); PowerManager.WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MyCpuLock"); wl_cpu.acquire(10000); } } 时如何替换名称?

1 个答案:

答案 0 :(得分:1)

首先,你可以清楚地提到第二次调用函数会搞砸。其次,你的代码中没有任何功能,所以你需要告诉哪一行搞砸了。我在Rory McCrossan的帮助下理解了这个问题,这就是答案。

我更改了代码并使用此RegEx工作:



var str = "Name: ";
str = str.replace(/Name:.*/gi, "Name:" + "Joe");
console.log(str);
str = str.replace(/Name:.*/gi, "Name:" + "Prav");
console.log(str);




RegEx的说明

preview

psst:没有比RegEx101更好的解释......