How to retrieve certain text from a string using regex and javascript? - TagMerge
4How to retrieve certain text from a string using regex and javascript?How to retrieve certain text from a string using regex and javascript?

How to retrieve certain text from a string using regex and javascript?

Asked 1 years ago
4 answers

<!DOCTYPE html>
 var s = `apiVersion: v1
 - cluster:
     certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ
     server: https://api.someaddress
 name: https://api.anotheraddress
 - context:
     cluster: https://api.another1address
     namespace: name1
     user: user1
  name: somename
 current-context: some-context
 kind: somekind
 preferences: {}
 - name: user-admin
         token: eyJhb`;
 var matchServer = new RegExp(/server: ([A-Za-z:.//]*)/, "i").exec(s);
 var matchNamespace = s.match(/namespace: ([A-Za-z0-9]*)/i);
 document.getElementById("serverToken").innerHTML = matchServer[1];
 document.getElementById("namespaceToken").innerHTML = matchNamespace[1];
<p id="serverToken"></p>
<p id="namespaceToken"></p>

js fiddle
You can be more specific in the yaml file search patterns by setting matchServer and matchNamespace as below:

var matchServer = new RegExp(/- cluster:\n[A-Za-z0-9\- :]*\n[ ]+server: ([A-Za-z:.//]*)/, "gi").exec(s);
var matchNamespace = s.match(/contexts:\n- context:\n[A-Za-z0-9\- :.//]*\n[ ]+namespace: ([A-Za-z0-9]*)/gi)

Source: link


const re = /ab+c/;
const re = new RegExp('ab+c');
If escape strings are not already part of your pattern you can add them using String.replace:
function escapeRegExp(string) {
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
In the following example, the script uses the exec() method to find a match in a string.
const myRe = /d(b+)d/g;
const myArray = myRe.exec('cdbbdbsbz');
If you do not need to access the properties of the regular expression, an alternative way of creating myArray is with this script:
const myArray = /d(b+)d/g.exec('cdbbdbsbz');
// similar to 'cdbbdbsbz'.match(/d(b+)d/g); however,
// 'cdbbdbsbz'.match(/d(b+)d/g) outputs [ "dbbd" ]
// while /d(b+)d/g.exec('cdbbdbsbz') outputs [ 'dbbd', 'bb', index: 1, input: 'cdbbdbsbz' ]

Source: link


To put this into practice - let's write a Regular Expression that checks if a string contains at the end of the string and contains three characters a before the @ symbol:
With this RegEx, we can match strings such as:
[email protected]
[email protected]
But not:
[email protected]
[email protected]
[email protected]
Using a RegEx literal, which is a pattern put between the / characters:
let regex = "/[abc]+/";
Using the RegExp() constructor:
let regex = new RegExp("[abc]+");

Source: link


let str = "I love JavaScript";

let result = str.match(/Java(Script)/);

alert( result[0] );     // JavaScript (full match)
alert( result[1] );     // Script (first capturing group)
alert( result.length ); // 2

// Additional information:
alert( result.index );  // 7 (match position)
alert( result.input );  // I love JavaScript (source string)
let str = "I love JavaScript";

let result = str.match(/Java(Script)/g);

alert( result[0] ); // JavaScript
alert( result.length ); // 1
let str = "I love JavaScript";

let result = str.match(/HTML/);

alert(result); // null
alert(result.length); // Error: Cannot read property 'length' of null
let result = str.match(regexp) || [];
let str = '<h1>Hello, world!</h1>';
let regexp = /<(.*?)>/g;

let matchAll = str.matchAll(regexp);

alert(matchAll); // [object RegExp String Iterator], not array, but an iterable

matchAll = Array.from(matchAll); // array now

let firstMatch = matchAll[0];
alert( firstMatch[0] );  // <h1>
alert( firstMatch[1] );  // h1
alert( firstMatch.index );  // 0
alert( firstMatch.input );  // <h1>Hello, world!</h1>

Source: link

Recent Questions on javascript

    Programming Languages