2

Code for checking over 1,000 sentences

Does Javascript have a built in CSS index I can check a sentence against?

At the moment…

If I wanted to check the sentences below for CSS properties, I have to create an array with ……(( ALL )) the CSS properties. Then check each sentence against the entire array.




Array

css_checker = [
"width","height","background","background color", "text decoration line", "right",
"table layout", "page break before", //and on… and… on and on………………

// I really don't want to use this array
// Using this is like a last resort
// I was hoping for a better way than this
]

Input

a = "A CSS property named animation fill mode is in this string"

b = "There are no CSS properties in this string"

c = "Width, height, and animation properties are in this string"

d = "Column rule width and transform origin are in this string"


Matchs

a: A CSS property named animation fill mode is in this string

b: There are no CSS properties in this string

c: Width, height, and animation properties are in these string

d: Column rule width and transform origin are in these string


Output

a: true

b: false

c: true

d: true

How to use it

if ( /*Sentence has CSS property*/ ) {
        //run this code
}


What I'm trying to do is

Find out where the first CSS property is located… and then split it at that point.

Example


Input

A CSS property named animation fill mode and width is in this string

Match

A CSS property named animation fill mode and width is in this string

Output

animation fill mode and width is in this string

2 Answers 2

5

You can use some in this to test the strings. If you're grabbing from element text, just pass in element.textContent.

function containsCSSProp(str) {
    css_checker = ["width","height","background","background color", "text decoration line", "right", "table layout", "page break before", "animation", "column rule width", "transform origin"];
    return css_checker.some(prop => str.toLowerCase().includes(prop));
}

console.log(containsCSSProp("A CSS property named animation fill mode is in this string"));
console.log(containsCSSProp("There are no CSS properties in this string"));
console.log(containsCSSProp("Width, height, and animation properties are in this string"));
console.log(containsCSSProp("Column rule width and transform origin are in this string"));

Sign up to request clarification or add additional context in comments.

2 Comments

I updated the question… can you check the What I'm trying to do is section
Also… performance wise… if I was checking 10,000 sentences… is this a good method?
0

let css_checker = ['width','height','background','background color', 'text decoration line', 'right','table layout', 'page break before'];
let a = 'A CSS property named animation fill mode is in this string';
let b = 'There are no CSS properties in this string';
let c = 'Width, height, and animation properties are in this string';
let d = 'Column rule width and transform origin are in this string';

for (let i = 0, max = css_checker.length; i < max; i++) {
    css_checker[i] = css_checker[i].replace(/\s/g, '');
}

function checkString(str, cssRules) {
    let string = str.replace(/\s/g, '');

    for (let i = 0, max = cssRules.length; i < max; i++) {
        if (string.toLowerCase().indexOf(cssRules[i]) !== -1) {
            console.log('true');
            return;
        }
    }

    console.log('false');
}



checkString(a, css_checker);
checkString(b, css_checker);
checkString(c, css_checker);
checkString(d, css_checker);

Another variant for you! It takes 8ms for 10 000 sentences and it's faster then the first answer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.