Skip to content

Instantly share code, notes, and snippets.

@manhnguyenv
Last active August 8, 2019 03:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manhnguyenv/005c4d945ca6a4ca70d5f17c4313bf6c to your computer and use it in GitHub Desktop.
Save manhnguyenv/005c4d945ca6a4ca70d5f17c4313bf6c to your computer and use it in GitHub Desktop.
Learn String JavaScript
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script>
console.log(stringContainString(["Alien", "line"])); // true
console.log(stringContainString(["zyxwvutsrqponmlkjihgfedcba", "qrstu"])); // true
console.log(stringContainString(["Hello", "hey"])); // false
function stringContainString(arr) {
// Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array.
return arr[1].toLowerCase()
.split('')
.every(function (letter) {
return arr[0].toLowerCase()
.indexOf(letter) != -1;
});
}
function replaceHtmlTag(originalString) {
var strippedString = originalString.replace(/(<([^>]+)>)/ig, "");
return strippedString;
}
function aContainsB(a, b) {
return a.indexOf(b) >= 0;
}
var philosophers = "Aquinas, Maimonedes, and Avicenna";
//var me = "Aquinas";
var me = "Joshua";
function printPhilosopherStatus(person) {
if (aContainsB(philosophers, person)) {
console.log(person + " is a philosopher.");
} else {
console.log(person + " is NOT a philosopher.");
}
}
function extractString(passedHtml) {
var stringIsHtmlSource = isHtml(passedHtml);
console.log(stringIsHtmlSource);
};
function isHtml(testString) {
return /(<([^>]+)>)/ig.test(testString);
};
window.onload = function () {
//var a = 'abc';
//var a = '<div>abc</div>';
//extractString(a);
// Outputs: "Joshua is NOT a philosopher."
printPhilosopherStatus(me);
};
</script>
</head>
<body>
<p>
<a href="https://css-tricks.com/snippets/javascript/strip-html-tags-in-javascript/">Strip HTML Tags in JavaScript</a>
</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment