How to connect javascript var with text? - javascript

How to connect javascript var xx with /\ and \b/ ?
https://jsfiddle.net/6r4o5278/6/
<div onclick="check()">CLICK HERE</div>
<script>
function check() {
var str = "abcdefg";
var xx = "abc";
if (/\+xx+\b/.test(str))
{
alert("found");
} else
{
alert("not found");
}
}
</script>

What do you mean by 'connect javascript var xx with /\ and \b/'. Furthermore see:
http://www.w3schools.com/jsref/jsref_regexp_test.asp
// The string:
var str = "Hello world!";
// Look for "Hello"
var patt = /Hello/g;
var result = patt.test(str);
// Look for "W3Schools"
patt2 = /W3Schools/g;
result2 = patt2.test(str);

Declare your regex before testing.
....
var xx = /abc\b/;
if (xx.test(str)) {
...
Note that this will not match the string "abcdefg" because \b is a Word Boundary flag.

use this one surely work...:)
var test = '/\\' + xx + '\\b/';
console.log(test);
it will be /\abc\b/.

Try to construct the expression this way:
<script>
function check(value) {
var str = "abcdefg";
var re = new RegExp(value);
var found = str.match(re);
if (found) {
console.log(value + " - found");
}
else
{
console.log(value + " - not found");
}
}
check('xsxsd');
check('abc');
check('refer');
check('cde');
</script>

Related

Concatenate different variable if exist in JS

I want to concatenate different variable if exist and return the result.
But I need to separate the value by "-" and return only the variable with value.
So I have this:
var test = "";
var test1 = "";
var test2 = "";
var test3 = "";
var checkTrue = "Rightt";
var checkFalse = "Wrong";
var checkWord = "Helloo";
var checkWord2 = "Bye";
if(checkTrue === "Right"){
test = "This is right"
} else {
test = "";
}
if(checkFalse === "Wrong"){
test1 = "This is wrong"
} else {
test1 = "";
}
if(checkWord === "Hello"){
test2 = "The word is hello"
} else {
test2 = "";
}
if(checkWord2 === "Bye"){
test3 = "The word is hello"
} else {
test3 = "";
}
var result = test + " - " + test1 + " - " + test2 + " -" + test3;
console.log(result);
So I think I need to verify before my var result, if the different variable exists ?
Thank for your help
Actual result:
- This is wrong - -The word is hello
Expected result :
This is wrong - The word is hello
With anything where you want a separator between string values, I find it easier to use Array.prototype.join() which will only place the separator between actual values.
It then becomes easier as you have a single results array that can be joined, only adding values you want rather than testing each of those variables you no longer need.
By pushing on to an array you can also get rid of all those test variables too.
const results = [];
const checkTrue = "Rightt";
const checkFalse = "Wrong";
const checkWord = "Helloo";
const checkWord2 = "Bye";
if (checkTrue === "Right")
results.push("This is right")
if (checkFalse === "Wrong")
results.push("This is wrong")
if (checkWord === "Hello")
results.push("The word is hello")
if (checkWord2 === "Bye")
results.push("The word is hello")
const result = results.join(" - ");
console.log(result);
const test = '';
const test1 = 'This is wrong';
const test2 = '';
const test3 = 'The word is hello'
const result = [test, test1, test2, test3].filter(value => value).join(' - ');
console.log(result);

Google Apps Script Auto Capitalise

I have been struggling with a script I have been working on for a while. The problem is that when I run the script with text like:
Hi. apples are amazing.
I want to make only the a in apples capitalized, but instead the text comes out as:
Hi. Apples Are Amazing.
Here is the code:
function caps()
{
var body = DocumentApp.getActiveDocument().getBody();
var text = body.editAsText()
var caps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var lower = "abcdefghijklmnopqrstuvwxyx";
//while (() != null) {
//var search = text.findText('. a')
//var start = replace - 1
//text.deleteText(start, replace)
//text.insertText(start, " A")//}
for(var i=0; i<caps.length; i++)
{
var nextCaps = caps.charAt(i);
var nextLower = lower.charAt(i);
while (text.findText('. ' + nextLower) != null)
{
Logger.log(nextLower)
Logger.log(nextCaps)
var search = text.findText('. ' + nextLower)
var replace = search.getEndOffsetInclusive()
var start = replace - 1
text.deleteText(start, replace)
text.insertText(start, " " + nextCaps)
}
//var nextChar = caps.charAt(i);
//Logger.log(nextLower)
}
}
Basically, the code looks for ". a" and replaces it with ". A" (same with b, c, d and so on). If anyone can help me with this it would be very much appreciated.
The below code follows your example where you want to capitalize the first letter after the end of the first sentence. So long as that is the case this regex and replace will do that for any letters.
var str = 'Hi. apples are amazing.';
var reg = /\.\s./;
function replacement(match) {
return match.toUpperCase();
}
str = str.replace(reg, replacement);
Logger.log(str);

Limiting length of each word in a string

Is there a way to limit the length of each word in a string?
For example:
Loop through each word in a string
If a word is longer than X amount of characters, display a pop up message and do not submit the form.
Edit: My final code:
$("#comment-form").submit(function(event) {
var str = $("#comment-box").val(), limit = 135;
var wordList = str.split(' ');
$(wordList).each(function(i, word) {
if(word.length > limit) {
alert("Your comment has a string with more than " + limit + " characters. Please shorten it.");
event.preventDefault();
}
});
});
Try this:
var str = "This is the test string that contains some long words";
var wordList = str.split(' ');
var limit = 4;
$(wordList).each(function(i, word){
if(word.length >= limit){
alert(word);
}
});
You can use the following function
<script>
var string = "Please be sure question to answer the question";
function checkWordLength(string)
{
var string_array = string.split(" ");
for(var i=0; i<string_array.length; i++)
{
var word = string_array[i];
var word_length = word.length;
if(word_length>6) return false;
}
}
checkWordLength(string);
</script>
jsFiddle
function CheckString(string, character_limit)
{
var word = /\w+/igm;
var match;
while((match = word.exec(string)) !== null) {
if(match[0].length > character_limit)
{
alert(match[0]);
return false;
}
}
return true;
}
var character_limit = 5;
var string = 'this is a string of words and stuff even';
CheckString(string, character_limit);
This example uses regular expressions when it returns false make sure to either return false from the onSubmit method of your form.

regex javascript- error

Why isn't this regular expression working? A correct email address does not pass the validation.
<script type="text/javascript">
$(document).ready(function() {
var regex = new RegExp(/^([\w-]+(?:\.[\w-]+)*)#((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i );
$('#submit').click(function () {
var name = $('input[name=name]');
var email = $('input[name=email]');
var website = $('input[name=website]');
var comment = $('textarea[name=comment]');
if ((!regex.test(email))) {
email.addClass('hightlight');
return false;
} else
email.removeClass('hightlight');
}
}
}
link:
http://emprego.herobo.com/
You are calling the RegExp test method on a jQuery object instead of a string. Change your conditional from:
if ((!regex.test(email))) { ... }
to:
if ((!regex.test(email.val()))) { ... }
and it should work.
For what email address are they failing? This regex appears to work:
var regex = new RegExp(/^([\w-]+(?:\.[\w-]+)*)#((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i );
var emails = [
'a.valid.email#gmail.com',
'asdf#asdf.com',
'another#valid.email.address.com'
];
var str = '';
for (var i=0; i<emails.length; i++) {
str += emails[i] + ': ' + regex.test(emails[i]) + "\n";
}
alert(str);
This produces an alert with "true" for each email.

Regex: Removes all HTML Tags and returns just the text?

i want to strip just text values from below html with js.
var Str = "<span style="">MY name is KERBEROS.</span><B>HELLO Everbody</B>"
All text strips with codes that is below;
[^<>]+(?=[<])
But i want to strip just UPPERCASE words. Clresult must be: MY, KERBEROS, HELLO
Thank you already now for your suggestions.
Regards,
Kerberos
Here is your code, gives output: MY,KERBEROS,HELLO
<script>
String.prototype.stripHTML = function()
{
var matchTag = /<(?:.|\s)*?>/g;
return this.replace(matchTag, "");
};
String.prototype.getUpperCaseWords = function()
{
var matchTag1 = /\b([A-Z])+\b/g;
var o = this.match(matchTag1);
return o;
};
var Str = "<span style=''>MY name is KERBEROS.</span><B>HELLO Everbody</B>";
var out1 = Str.stripHTML();
var out2 = out1.getUpperCaseWords();
alert(out2);
</script>
This is another solution in JavaScript using just one line of regex:
String.prototype.stripHTML = function()
{
var matchTag = /<(?:.|\s)*?>/g;
return this.replace(matchTag, "").match(/\b([A-Z])+\b/g);
};
var Str = "<span style=''>MY name is SATYA PRAKASH.</span><B>HELLO Everyone</B>";
var out1 = Str.stripHTML();

Categories