Javascript
 Computer >> コンピューター >  >> プログラミング >> Javascript

JavaScriptには、新しい文字列を作成せずに文字列の一部を置き換えるメソッドがありますか?


はい、次の構文のようにreplace()メソッドを使用して、新しい文字列を作成せずに文字列の一部を置き換えることができます-

var anyVariableName=yourValue;
yourVariableName=yourVariableName.replace(yourOldValue,yourNewValue);

var message="Hello,this is John";
console.log("Before replacing the message="+message);
message=message.replace("John","David Miller");
console.log("After replacing the message="+message);
を置き換えた後

上記のプログラムを実行するには、次のコマンドを使用する必要があります-

node fileName.js.

ここで、私のファイル名はdemo57.jsです。

出力

これにより、次の出力が生成されます-

PS C:\Users\Amit\JavaScript-code> node demo57.js
Before replacing the message=Hello,this is John
After replacing the message=Hello,this is David Miller

  1. 新しいキーワードでJavaScript配列を作成します。

    以下は、新しいキーワードを使用してJavaScript配列を作成するためのコードです- 例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style&

  2. 「new」演算子を使用してJavaScriptコンストラクターを作成しますか?

    以下は、「new」演算子を使用してJavaScriptコンストラクターを作成するためのコードです- 例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <