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
0
4 answers

<!DOCTYPE html>
<html>
<head>
<script>
 var s = `apiVersion: v1
 clusters:
 - cluster:
     certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ
     server: https://api.someaddress
 name: https://api.anotheraddress
 contexts:
 - context:
     cluster: https://api.another1address
     namespace: name1
     user: user1
  name: somename
 current-context: some-context
 kind: somekind
 preferences: {}
 users:
 - name: user-admin
     user:
         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];
</script>
</head>
<body>
<p id="serverToken"></p>
<p id="namespaceToken"></p>
</body>
</html>

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

0

const re = /ab+c/;
RegExp
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

0

To put this into practice - let's write a Regular Expression that checks if a string contains @gmail.com at the end of the string and contains three characters a before the @ symbol:
"\w+a{3}@gmail\.com"
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

0

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